61 of 208
ZBasic Language Reference 53 ZX Microcontroller Family Chapter 3 - Advanced Topics This chapter provides additional technical information on topics that were introduced earlier in this document. Also, some more advanced concepts are introduced. 3.1 Scope and Lifetime There are two important attributes of variables that have been alluded to in earlier discussion – scope and lifetime. The scope of a variable reflects its visibility in the sense of where it can be accessed by name. A variable defined within a subroutine or function has local scope meaning that it is only directly accessible to code within that routine. A variable defined outside of any routine has module scope if it is declared Private and global scope if it is declared Public. Module scope means that only routines within that module can access it directly. Global scope means that any routine in the application can access it directly. These three scoping levels, global, module and local, form a hierarchy that controls the visibility of the variables. Global scope is at the outermost level of the hierarchy, module scope is at the next inner level and local scope is at the next inner level to that. There are additional inner levels of scope created by compound statements, which topic is discussed further below. At any particular level of the scoping hierarchy, variables that are in the same scope level or farther outward are visible. It is possible to define variables with the same name at different scoping levels. This does not cause a conflict because the compiler resolves a reference to a particular variable name by searching the current scoping level first and then proceeding outward in the hierarchy until the variable name is found or not as the case may be. Variables are said to “hide” same-named variables that exist at outer scoping levels. In most cases, the hidden variables can still be accessed but more information has to be added to the variable name to clarify to the compiler which variable is being referenced. This concept may be clarified by an example. Example Module T1: Public i as Integer Module T2: Private i as Integer Sub foo() Dim i as Integer i = 5 ' this refers to the locally defined variable t2.i = 5 ' this refers to the private variable at the module level t1.i = 5 ' this refers to the public variable in module T1 End Sub The second and third references to the variable i in the example above are qualified by the addition of the module name containing the definition of the desired variable. You may add module qualification to any module level and global level variable reference if you wish but it is generally only done when required to resolve the reference to the intended variable. Although the preceding discussion focused on variables, the same scope concept applies to all identifiers – variables, constants, subroutines and functions. A local constant named count will hide a module level variable of the same name. Because the inadvertent hiding of identifiers is a common cause of programming errors the compiler, by default, issues a warning about the hiding. The warning can be disabled if desired. See Section 8.2 for specific information on various compiler options.