| Applying science to business management |
  
Variable Scope
A variable declared inside a node definition is visible only
to other statements inside the same definition. This property
allows you to use the same variable name inside more than one
definition without worrying about the names conflicting. For
example, the following definitions both use a variable named y.
However, since the variables are declared inside separate node
definitions, they are completely unrelated. You can change the
value of one instance of y without affecting the other.
Result:={
var x=5,y=0; // declare y and set = 0
f(x); // f() declares a new y
watch(y); // original y is still
= 0
}
f(x):={
var y=x*x; // declare new y
watch(y); // y = 25
}
To be more precise, it is not the fact that the variables are
declared inside definitions that makes them local to that
definition. It is because they are declared inside braces { }. By
using braces inside a single definition as follows, you can
accomplish the same function as the two definitions above.
Result:={
var x=5,y=0; // declare y and set = 0
{
var y=x*x; // declare new y
watch(y); // y = 25
}
watch(y); // original y is still = 0
}
Note that y is declared in two places inside the same
definition. However, because the second declaration is enclosed
in braces, it is completely separate from the first. Also, note
that even though x is declared outside the inner set of
braces, it is still visible inside the inner braces.
The rules for variable scope are really quite simple. Any
variable declared inside braces is visible only inside those
braces. In addition, any variable declared inside braces is
visible EVERYWHERE inside those braces, including inside nested
braces. Here is an example:
Result:={ // begin a new block
scope
var x=1,y=2; // create variables
x and y
{ // begin a second
block scope
var y=3,z=4; // create
new y and z
watch(x,y,z); // x =
1, y = 3, z = 4
x=5; // set original
x equal to 5
} // delete new y and
z
watch(x,y); // x = 5, y = 2, z no
longer exist
} // delete x
and y
The first var statement
creates the variables x and y and initializes their
values to 1 and 2 respectively. The second var
statement creates a new instance of y that is distinct
from the original y. Even though the second var statement sets the value of y
to 3, the original y still has a value of 2. However, you
cannot access the original y because it has been declared
a new variable with the same name that shadows the original. Note
that this is not true for x. Inside the second set of
braces, the variable x is still accessible. Because of
this, you can change the value of x. When you exit the
nested braces, the change made to the value of x persists.
However, the change made to y does not persist because the
change was made to a different instance of y, and the
changed y no longer exists. Similarly, the variable z
that was defined inside the nested braces no longer exists.
Instead of using braces to enclose a set of statements, you
can use parentheses ( ). Parentheses and braces function in
almost exactly the same way. The only difference is that
parentheses do not create a new block scope. For example, if you
change the inner set of braces to parentheses, the new
declaration for y replaces the original declaration.
Result:={ // begin a new block
scope
var x=1,y=2; // create variables
x and y
(
var y=3,z=4; // replace y, create z
watch(x,y,z); // x =
1, y = 3, z = 4
x=5; // set original
x equal to 5
);
watch(x,y,z); // x = 5, y = 3, z = 4
} // delete x, y, and z
You can replace the outer set of braces with parentheses as
well. If you do this, the variables x and y will be
declared as global variables. This means that they will continue
to exist even after execution of the node Result has
finished and they can be accessed by other functions.
Result:=( // remain in global
scope
var x=1,y=2; // create global x
and y
{ // begin a new block
scope
var y=3,z=4; // create
local y and z
} // delete local y
and z
) // global x
and y still exist
|