Local Variables
Local variables are restricted in scope to the statement block in which they are declared. A statement block is one or more NetLinx statements enclosed in a pair of braces, like the blocks following subroutines, functions, conditionals, loops, waits, and so on.
Local variables must be declared immediately after the opening brace of a block but before the first executable statement. To provide compatibility with the Axcess language, local variables may be declared right before the opening brace for DEFINE_CALL declarations only. For example, both formats shown below are legal in the NetLinx language:
The scope of a local variable is restricted to the statement block in which it is declared. A local variable is either static or non-static, depending on whether it is declared as LOCAL_VAR or STACK_VAR:
- The keyword LOCAL_VAR specifies a static variable. A static variable's value is initialized the first time the statement block in which it is declared is executed and retained after execution of the statement block has finished.
- The STACK_VAR keyword specifies a non-static variable. A non-static variable's value is re-initialized every time the statement block in which it is declared is executed.
- If neither the LOCAL_VAR nor the STACK_VAR keyword is specified, STACK_VAR is assumed (default).
Example:
LOCAL_VAR and STACK_VAR can be used interchangeably in any statement block except for waits. Only LOCAL_VAR variables may be declared inside a wait block.
Example:
A name assigned to a local variable must be unique within the statement block in which it is declared and any statement block enclosing that block. Therefore, non-nested statement blocks can define the same local variable name without conflict.
Example:
If a local variable shares the same name as a global variable, the local variable always takes precedence.
The general form of a static local variable declaration is:
The general form of the non-static local variable declaration is:
Since non-static local variables are allocated on the program stack (a block of memory reserved for allocation of temporary variables), the keywords VOLATILE, PERSISTENT and CONSTANT do not apply.