Product SiteDocumentation Site

11.3.2.6. Variable Scope

A variable is only valid within its "scope." A variable's scope is determined by where it is declared. It will always last between either ( and ) or { and }, and applies to all statements within that block of code. Variable names can be re-declared in some contexts, which can be confusing.
Consider the scope of the variables in this example:
(
   var zero = 0;
   var function =
   {
      var zero = 8;
      var sixteen = 16;
      zero.postln; // always prints 8
   };

   function.value;
   zero.postln; // always prints 0
   sixteen.postln; // always causes an error
)
Because function declares its own copy of zero, it is modified independently of the variable zero declared before the Function. Every time function is executed, it re-declares its own zero, and the interpreter keeps it separate from any other variables with the same name. When function has finished executing, the interpreter destroys its variables. Variables declared inside any Function are only ever accessible from within that Function. This is why, when we try to execute sixteen.postln;, the interpreter encounters an error: sixteen exists only within function, and is not accessible outside the function. By the way, in order to excute this example, you will need to remove the error-causing reference to sixteen.
Now consider the scope of the variables in this example:
(
   var zero = 0;
   var function =
   {
      var sixteen = 16;
      zero = 8;
      zero.postln; // always prints 8
      sixteen.postln;
   };

   function.value;
   zero.postln; // always prints 8
)
Why does the last line always print 8? It's because zero was set to 8 within function. More importantly, function did not declare its own copy of zero, so it simply accesses the one declared in the next "highest" block of code, which exists between ( and ) in this example.
This is why it is important to pay attention to a variable's scope, and to make sure that you declare your variables in the right place. Unexpected and difficult-to-find programming mistakes can occur when you forget to declare a variable, but it is declared elsewhere in your program: you will be allowed to use the variable, but it will be modified unexpectedly. On the other hand, it can be greatly advantageous to be able to access variables declared "outside the local scope" (meaning variables that are not declared in the same code block in which they are used), but careful thought and planning is required.
Astute readers will notice that it is possible to re-declare the single-letter variable names, allowing you to control their scope. Consider the following program:
(
   var a = 0;
   b =
   {
      var c = 16;
      a = 8;
      a.postln;
      c.postln;
   };

   b.value;
   a.postln;
)
This example requires careful examination. What is the scope of a, b, and c? The answers may be surprising.
  • a is declared just after the ( character, so the interpreter destroys it upon reaching the ) character.
  • c is declared just after the { character, so the interpreter destroys it upon reaching the } character.
  • b is not declared in this program, so it refers to the automatically-declared variable with that name. The interpreter does not destroy it until it is restarted or stopped. This means that the Function assigned to b is still available after the program finishes execution. Try it! Execute the program above, and then execute this single-line program alone: b.value;