Scope#
Try this!
What will this print? Copy it over to your Processing IDE and find out what it prints!
Hint
IT PRINTS NOTHING. IT IS A FULL AND COMPLETE FAILURE. Processing won't even try to compile it, so great it the degree of my failure. And that was intentional. Learn from my mistake.
String myLittleFunction() {
String friendship = "Magic";
return friendship;
}
void setup() {
myLittleFunction();
println(friendship);
}
Global and Local Variables#
So far, our variables have usually been global variables. We declared them somewhere outside of any function, usually at the top of our code. In the example above, friendship
is local to the myLittleFunction()
. It only exists inside that function and cannot be used outside. This may seem inconvenient at first, but it's absolutely necessary for even slightly larger projects.
Run Me
try this code in your processing IDE and see what it does.
int counter = 0;
void draw() {
background(0);
counter++;
text(counter, 20, 20);
}
void mousePressed() {
counter = 0;
}
This is a classic example of a global variable. Our counter
can be used in setup()
and in mousePressed()
. We can read and write its value wherever we want. Let's make one tiny change and see what happens:
Make that change!
What happens with this tiniest of changes? How does it behave now? Why?
int counter = 0;
void draw() {
background(0);
counter++;
text(counter, 20, 20);
}
void mousePressed() {
int counter = 0;
//---- we just added this tiny `int`
// here, nothing more.
}
counter
is now no longer the same as counter
. That's counterintuitive, I know. We now have a global variable called counter which is used in draw()
and we also have our own local variable counter
that's just valid within mousePressed()
. This is, why clicking does not change the number any more. We do set a variable to 0
on every press of the mouse button, it's just not the same variable we use in draw()
any longer.
Why even do this?#
In our quest to make our code more readable, it is often a good idea to keep all the related parts close together. Imagine having a somewhat larger game in Processing with a thousand lines of code. If all your variables were global, these things would start happening:
- You would quickly run out of good names
- You would accidentally alter variables that you did not mean to alter
Local variables are a way to prevent these things from happening.
As Local as Possible
In our projects, there will usually be a few global variables. Whenever possible, it's a good idea to use local variables instead. Need a String
just to puzzle together a reply to display? That can be a local variable. Want to have intermediate steps in a complicated calculation? Probably just as good with a local variable.
Think of global variables as "stuff that's lying around everywhere". Don't clutter up your global scope with unnecessary stuff!
A Tale of Two Results#
float addition(float a, float b) {
float result = a + b;
return result;
}
float subtraction(float a, float b) {
float result = a - b;
return result;
}
void setup() {
println(addition(5, 2));
println(subtraction(2, 1));
}
This code has two local result
variables. That is completely fine, they are only local to their functions and don't interfere with each other.
Relevant excerpt from Learning Processing#
This explanation is part of "loops" which we'll get to next week, but it is still applicable here.
(the section starts at 2:57:50 and runs through 3:00:10, the video should start and stop on these automatically.)