user63689: const int val3 = 7;
void setup(){
  Serial.begin(9600);
  int val3 = 8;
  int val4 = 4;
  Serial.print(calc(val3,val4));
}

void loop() 
{}

int calc(int val1, int val2)
{
  return val1 + val2;
}

this code gives output as 12.

user62523: In your code, the const int val3 = 7; definition, is outside of any functions so it defines a global constant/variable (available in every part of your code).
The int val3 = 8; definition is inside your setup () function, so it acts as a local variable only within that function. This overrides the val3 constant defined outside of the setup () function. I.e. the global val3 constant and the local val3 variable inside of setup () are not the same variable, which will inevitably lead to confusion (as in your case).
It’s valid syntax but I don’t consider it good coding practice. It’s good to know why it works that way though.

63689: <pre><code>const int val3 = 7;
void setup(){
  Serial.begin(9600);
  int val3 = 8;
  int val4 = 4;
  Serial.print(calc(val3,val4));
}

void loop() 
{}

int calc(int val1, int val2)
{
  return val1 + val2;
}
</code></pre>

<p>this code gives output as 12.</p>
 62523: <p>In your code, the <code>const int val3 = 7;</code> definition, is outside of any functions so it defines a <strong>global</strong> constant/variable (available in every part of your code).</p>

<p>The <code>int val3 = 8;</code> definition is inside your <code>setup ()</code> function, so it acts as a <strong>local</strong> variable only within that function. This overrides the <code>val3</code> constant defined outside of the <code>setup ()</code> function. I.e. the global <code>val3</code> constant and the local <code>val3</code> variable inside of <code>setup ()</code> are not the same variable, which will inevitably lead to confusion (as in your case).</p>

<p>It’s valid syntax but I don’t consider it good coding practice. It’s good to know why it works that way though.</p>
