local !variable [= s_exp] [, !variable [= s_exp]...];
This statement is provided to allow other grammars to implement local variables within functions. It defines new local variables in the current scope, overriding any outer scope that may also define those variables. Each local variable consists of a variable name (a symbol) and an optional initial value.
To test whether a symbol is bound in the current scope, use the predicate undefined_p.
Gamma> i = 5; 5 Gamma> function print_three_local () { local i; for(i=1;i<=3;i++) { princ("value of i is: ", i, "\n"); } } <function definition> Gamma> function print_three_global () { // local i; for(i=1;i<=3;i++) { princ("value of i is: ", i, "\n"); } } <function definition> Gamma> i; 5 Gamma> print_three_local(); value of i is: 1 value of i is: 2 value of i is: 3 3 Gamma> i; 5 Gamma> print_three_global(); value of i is: 1 value of i is: 2 value of i is: 3 3 Gamma> i; 4 Gamma>
This example shows the variable i receiving the value of 5. The two functions are defined identically, except for their names and where the second function comments out the 'local' command. When the first function is run the internal scoping using the local directive protects the value of i globally. When the function returns, i remains at 5, even though it was the value 1,2 and 3 within the scope of that function.
The second function has the 'local' directive commented out (using //), so the global variable i is modified. When the function returns and we check the value of i, it is 4. The 'for-loop' within the second function incremented the value of i until it failed the i<=3 comparison. After the second function is run the value of i is 4. The global variable i has not been protected in the second function.
Copyright © 1995-2010 by Cogent Real-Time Systems, Inc. All rights reserved.