You declare variables local using the local statement, which looks like:
local idwhere each id has one of the following two forms:, id
,
![]()
nameThe second form specifies an initial value to assign to the local variable. You can use any valid expression (Chapter 4, pagename := expression
If local is used outside of statement blocks and functions, then it creates a global variable. So in this example,
if ( x ) local a := 3the use of local is not in a statement block and not 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 the block. Subsequent uses of the variable a in this block would 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. Variable 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 would be:
80 10 90 90 -90 90 0 0 0The local declaration is also discussed in § 6.6.1.