Page 94 of 229 Start Back Next End ZBasic Language Reference 84 ZBasic Microcontrollers #pragma GlobalCallTargets ( <based-procedure> : <procedure-list> ) 3.23 Reference Variables Similar to the concept of a ByRef parameter for a subroutine or function, you may define a variable as a reference. A reference variable is interpreted as a “pointer to” the actual variable rather than the variable itself. Consequently, a reference variable is always the same size (typically 2 bytes) irrespective of the size of the variable to which it refers. A reference variable is defined in the same way as an ordinary variable but including the ByRef attribute as illustrated in the examples below. The second example illustrates a reference to an array of bytes. As with a procedure parameter that is an array, an array defined by reference is also single dimensioned with a lower bound of 1. Note that a reference variable, unlike other variables, cannot have an initialization value specified in the definition. Dim fval as Single ByRef Dim buf() as Byte ByRef The compiler dereferences a reference variable when it is used just as it does with ByRef parameters so there are no extra syntactic elements required to perform dereferencing. This begs the question, however, of how the value of the reference gets set. When a reference variable is defined, the content of the space reserved for the reference is either uninitialized or set to zero depending on whether the definition is local to a procedure or at the module level. Since neither of these possible states is likely to refer to any useful memory, you must set the value of the reference before any use of the reference variable. Initialzation of the reference is accomplished using the DataAddress property as illustrated below. Dim f as Single Dim fval as Single ByRef fval.DataAddress = f.DataAddress After the reference is initialized, all uses of the name of the reference variable (without the .DataAddress qualifier) access the memory to which the reference refers. Note that the address reference operator (@) can be used with the variable name instead of the DataAddress property both for setting the reference address and for obtaining the address of an ordinary variable. The last line in the preceding example could thus be rewritten with equivalent effect in any of the three following ways. @fval = @f fval.DataAddress = @f @fval = f.DataAddress The similarities of the effects of a reference variable and a based variable are likely obvious. There are, however, some important differences that may recommend the use of one over the other depending on the circumstances. In particular, it is important to note that a reference variable may be used as a member of a structure or class whereas a based variable cannot. 3.24 Sub-byte Types In addition to the fundamental data types described in Section 2.2, ZBasic also supports Bit and Nibble data types. These are referred to as sub-byte types because they occupy less than a whole byte – 1 bit and 4 bits, respectively. In certain circumstances, these types may help reduce the total RAM usage of an application. As compared to packing and unpacking bits in your source code, using these types will be more efficient in both execution time and code space. Bit and Nibble types may be used in most places where one of the fundamental data types may be used. You can define arrays of them, you can define Bit and Nibble constants, you can pass them as parameters and you can define functions returning these types. One limitation is that you cannot pass a