int myVar = 10; //Global variable
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
int myVar = 5; //Local variable with the same name
Serial.print("Local myVar: ");
Serial.println(myVar); //Prints 5, referring to the local variable
/* If you declare a variable with the same name inside a function, this creates a
new, seperate variable that is local to that function. This local variable is only
accessible within the scope of the function where it's declared.
When you use the variable name inside the function, the Arduino compilier will
refer to the local variable, not the global one. This is because the local scope takes
precendence over the global scope.
The global variable is temporarily "hidden" within the function.
*/
}
void loop() {
// put your main code here, to run repeatedly:
delay(10); // this speeds up the simulation
Serial.print("Global myVar: ");
Serial.println(myVar); //Prints 10, referring to the global variable
// The local variable does not exist in this function, it only exists in setup()
delay(10000);
}