Getting Started | Documentation | Glish | Learn More | Programming | Contact Us |
Version 1.9 Build 1556 |
|
local id1, id2, ...Here each id has one of the following two forms:
name
name := expressionThe second form specifies an initial value to assign to the local variable. You can use any valid expression (See Chapter 4, page ). The assignment is done each time the local statement is executed.
If local is used outside of statement blocks and functions, it creates a global variable. So in this example,
if ( x ) local a := 3local is neither in a statement block nor inside a function, so the variable that is created, a, is a global variable. Global variables are accessible everywhere in Glish. This usage is equivalent to:
if ( x ) a := 3If the local declaration does not include any initializations then it is equivalent to an empty statement:
if ( x ) local ais the same as
if ( x ) ;If local is used inside of statement blocks, then the variable that is created is local to that block. So in this example,
if ( x ) { local a := 89 }because local is used inside of a statement block, the variable a is local to that block. Subsequent uses of the variable a in this block will refer to this local variable rather than one that might be defined in the global scope. If no initialization is provided, local still introduces a variable that is local to the statement block. So in this example:
a := b := c := 0 if ( T ) { local a := 90, c := 90 local b b := -90 { local b, a := a a := a - 10 b := 10 print a,b,c } print a,b,c } print a,b,cthe variables a, b, and c start out in the global scope with the value 0. Local variables are introduced as part of the if statement block, and finally another statement block is introduced inside the if block. Variables local to this block are again declared. The variable a in this case is initialized to the value that it had in a wider scope, the if block in this case, and it is then modified. Because it is declared to be local to this block, the variable a in the if block is not modified. The variable c, on the other hand, is not local to the inner block so c in the inner block is the same as the c in the if block. The final output of this code snippet is:
80 10 90 90 -90 90 0 0 0(See § 6.6.1 where this is also discussed.)