ZBasic Language Reference
49
ZX Microcontroller Family
There are restrictions on whether a particular variable type may, may not, or must be passed by value or
by reference. For example, a persistent variable cannot be passed by reference because the called
routine is expecting the address of a RAM-based variable. Also, for efficiency reasons some variables
are always passed to routines by providing the variable address, even if the definition of the routine
specifies the parameter is to be passed by value. In such cases, the compiler treats the parameter as
being read-only, effectively enforcing the semantics of pass-by-value. Any attempt to modify a read-only
variable in the called routine or to pass it by reference to another routine will be detected and reported as
an error by the compiler. Arrays may only be passed by reference and then only if they are RAM-based,
single-dimension and have a lower bound of 1.
Allowed Parameter Passing Methods
Actual Parameter Type
Pass By Value
Pass By Reference
Constant or expression, any type
Yes
No
RAM-based variable or array element
Yes¹
Yes²
RAM-based single-dimension array, 1-based
No
Yes
RAM-based single-dimension array, not 1-based
No
No
RAM-based multi-dimensional array
No
No
Persistent variable or array element
Yes
No
Persistent Memory array
No
No
Program Memory array element
Yes
No
Program Memory array
No
No
Notes:
¹ String types and structures are read-only within the called routine when passed by value.
² The sub-byte types, Bit and Nibble, cannot be passed by reference. Also, in BasicX mode,
UnsignedInteger and UnsignedLong types are read-only within the called routine when
passed by reference.
The table below gives the number of bytes of stack space required to pass different variable types using
the two passing methods. This table applies only to VM mode devices. For native mode devices,
parameters are passed in registers in most cases and, therefore, require no stack space.
Stack Usage by Parameter Type and Passing Method
Actual Parameter Type
Pass By Value
Pass By Reference
Boolean, Byte
1
2
Bit, Nibble
1
2²
Integer, UnsignedInteger, Enum
2
2
Long, UnsignedLong, Single
4
2
String¹, structure
2
2
Array, any type
n/a
2
Notes:
¹ Persistent strings, Program Memory strings and strings returned by functions all require 4 bytes of
temporary data space (local to the caller) plus the 2-byte reference when passed to a routine
other than a System Library routine.
² Sub-byte types like Bit and Nibble may only be passed by reference if they are byte aligned. See
2.15 Program and Data Item Properties
Most data items, whether located in RAM, Persistent Memory or Program Memory have an associated
property called DataAddress that evaluates to the address of the data item. The DataAddress property is
applied to a data item by appending it to the data items name with a period separating them as illustrated
by the example below.