An Introduction To Programming With Processing

Variable Scope

Variable Scope
An important aspect of working with variables is that they can be accessed at various places in your program so that the values associated with them can be referenced (in their original form) or changed and then referenced. For example you can reference a variable in one statement, run a couple of other statements then choose to reference the original variable again even with all the code separating the the first reference from the second, Processing will still remember what the variable you are referring to is. But not every variable is accessible from every place within a program. Variable scope refers to the enclosing range within a program that a variable can be accessed. For example variables defined within the setup() function are not accessible to the rest of the program, which is why setup() is best suited for assignment in terms of dynamic programming. As such there is a scope in which variables can be defined such that they are accessible from every place within a sketch, we call this the global variable scope.
As you can see in the previous image x cannot be accessed from within draw() because it has been initialized within the setup() function. Initialization is the process of combining variable declaration and assignment into one statement. Take the following declaration and assignment statements for example:
int x;        //declaration
x = 20;     //assignment
or the statements could be combined in one statement and written as such:
int x = 20; //initialization is both declaration and assignment 
Although the latter statement might save you some time in terms of typing, it is something you should be careful of doing when taking variable scope into consideration, as when this method of initialization is used within setup() it is going to create variables that are not accessible (and therefore cannot be referenced) from outside of the setup() structure. The proper way to address this issue would be to declare the variable outside of both draw() and setup(). Generally it is not a good idea to declare a variable more than once because this could cause unnecessary overhead on your program, as such, declaring variables within the draw() structure is best avoided whenever possible. Nonetheless, declaring variables within the draw() structure sometimes cannot be avoided and as a result is considered to be valid code. Each time you use a datatyping keyword for variable declaration Processing thinks you are trying to create a new variable, and does not perceive the statement as updating the already existing variable with the same name. Programming like this tends to look ambiguous and difficult to read. However, as already mentioned, there are times when declaring a variable within draw() is unavoidable, or in some cases can even contribute to the readability of your code. Basically, what this boils down to is that where you choose to declare your variables is your choice, but there are also certain rules in place that if you are aware of, can make this decision somewhat easier for you. Lets restructure the previous example using a more readable format that takes variable scope into consideration:
int x;                      //declare variable only once in global variable scope 
  
void setup(){        /*assign a starting value to the variable. Note: accessible within setup()structure*/
  x = 20;             
  }                  

void draw(){        /*access the variable from elsewhere in the program.*/ 
  println(x);          /*Note: also accessible within draw() structure*/      
  }              
By declaring a variable outside of the setup() and draw() structures we have created a global variable. A global variable can be accessed from both the setup() and draw() structures, it is said to have a greater scope than a variable that is defined within the setup() or draw() structures. Within setup() the variable has been given a starting value, remember that the variable already exists before the program reaches setup(), so setup() is not being used to create or declare the variable only for assignment. By the time the program gets to draw the new value associated with the variable is the value that the println() function reads each time it repeats. It's also worth noting again that only the code block within the draw() function is repeated.