Programming Arduino (1) Pages 72
Programming Arduino (1) Pages 72
flash(x, 10);
This code would not result in a compilation error. However, you need to be
careful, because you now actually have two variables called x and they can each
have different values. The one that you declared on the first line is called a
global variable. It is called global because it can be used anywhere you like in
the program, including inside any functions.
However, because you use the same variable name x inside the function as a
parameter, you cannot use the global variable x simply because whenever you
refer to x inside the function, the “local” version of x has priority. The parameter
x is said to shadow the global variable of the same name. This can lead to some
confusion when trying to debug a project.
In addition to defining parameters, you can also define variables that are not
parameters but are just for use within a function. These are called local
variables. For example:
void indicate(int x)
int timesToFlash = x * 2;
flash(timesToFlash, 10);
The local variable timesToFlash will only exist while the function is running.
As soon as the function has finished its last command, it will disappear. This
means that local variables are not accessible from anywhere in your program
other than in the function in which they are defined.