94 of 208
ZBasic Language Reference 86 ZX Microcontroller Family #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. 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 Bit or Nibble variable to a subroutine/function by reference unless the variable is byte-aligned (see further discussion below). This implies, of course, that you may not be able to pass an array of sub-byte types as a parameter since arrays are always passed by reference. The reason for this limitation is that sub-byte variables do not necessarily begin on a byte boundary and there is no way for the called routine to know what the alignment might be. One way to work around this limitation is to define an integral-byte alias to the sub-byte type, pass the alias by reference and then define a new sub-byte alias in the called