We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1
So, for instance, the following example will cause an error:
void indicate(int x)
int timesToFlash = x * 2;
flash(timesToFlash, 10);
timesToFlash = 15;
Seasoned programmers generally treat global variables with suspicion. The
reason is that they go against the principal of encapsulation. The idea of encapsulation is that you should wrap up in a package everything that has to do with a particular feature. Hence functions are great for encapsulation. The problem with “globals” (as global variables are often called) is that they generally get defined at the beginning of a sketch and may then be used all over the sketch. Sometimes there is a perfectly legitimate reason for this. Other times, people use them in a lazy way when it would be far more appropriate to pass parameters. In our examples so far, ledPin is a good use of a global variable. It’s also very convenient and easy to find up at the top of the sketch, making it easy to change. Another feature of local variables is that their value is initialized every time the function is run. This is nowhere more true (and often inconvenient) than in the loop function of an Arduino sketch. Let’s try and use a local variable in place of a global variable in one of the examples from the previous chapter: // sketch 4-03