62 of 208
ZBasic Language Reference 54 ZX Microcontroller Family The second important attribute is lifetime. This concept refers to how long storage space is reserved for a variable. For variables defined at the module level, the lifetime is indefinite. They exist as long as the program is running. For variables defined within a routine, the lifetime normally begins when the routine begins execution and it ends when the routine finishes execution. Because these variables are dynamically created and destroyed, they are referred to as dynamic variables. This is in contrast to the module level variables which are static variables – they exist for the duration of the program’s execution. Sometimes, it is convenient to have a variable that is visible only to the routine in which it is defined but which is also static. ZBasic supports this concept by allowing the use of the keyword Static in place of the keyword Dim normally used in a variable definition within a routine. The Static keyword tells the compiler to allocate space for the variable alongside the module level variables but since it is defined within a routine, i.e., it has local scope, only the code in that routine can directly access the variable. Example Private Sub mySub() Dim var1 as Integer Static var2 as Integer [other code here] End Sub The difference between var1 and var2 is that space is allocated on the stack for var1 when the routine begins executing while the space for var2 exists as long the program is executing. Another difference is that dynamically allocated variables like var1 have an undefined value immediately after they are created, you have to add code to initialize them. By default, the compiler will issue a warning if you write code that uses the value of a dynamically allocated variable before it is initialized. Note that string variables are a special case in that they are automatically initialized to a zero length. In contrast, statically allocated variables like var2 and all module level variables are initialized to zero just before the Main() begins running. Each time mySub() is invoked, the value of var1 is undefined but the value of var2 is whatever was assigned to it last. A consequence of this difference comes into play if mySub() is recursively invoked. Each invocation of mySub() will have its own private version of var1 but they will all share the same var2. BasicX Compatibility Note In BasicX mode, variables cannot be defined as Static. For all compound statements (If-Then, Do-Loop, For-Next, Select-Case and While-Wend), you may define additional variables within the body of the compound statement. When this is done, those variables will only be directly accessible to statements within the compound statement, including any nested compound statements. This is another example of local scope described above. If a variable so defined has the same name as a variable defined in an enclosing compound statement, in the routine itself, or at the module level, the newly defined variable obscures the same-named variable defined at the outer level rendering it inaccessible by normal means. Example Module Test: Dim i as Integer Sub Main()