28 of 208
ZBasic Language Reference 20 ZX Microcontroller Family {Public | Private | Dim} <name>(<dim-spec-list>) As <type> The <dim-spec-list> is a list of up to 8 dimension specifications each separated from the next by a comma. A dimension specification consists of a constant expression giving the upper bound of elements along that dimension or two constant expressions separated by the keyword To specifying the lower bound and upper bound, respectively, of the elements along that dimension. When only the upper bound is given, the lower bound defaults to either zero or one depending on whether or not an Option Base directive is in effect or not. If no Option Base directive has been specified, the lower bound is zero. Note that in order to be passed as a parameter to a subroutine or function an array must have a lower bound of 1. For this reason it is probably more common to define arrays with a lower bound of 1. Many people find it easier to think about arrays this way as well. The default lower bound of zero is kept for compatibility reasons. Examples Option Base 0 Dim ival(5) as Integer This defines an array of Integer values with 6 elements. The lower bound is zero and the upper bound is 5. Option Base 1 ... Dim ival(5) as Integer This defines an array of Integer values with 5 elements. The lower bound is 1 and the upper bound is 5. The lower bound may be explicitly specified as well. The second example below illustrates the use of a negative value as one of the bounds. Note that the upper bound must be greater than or equal to the lower bound. Public pulseCount(1 to 6) as Byte Private itemData(-3 to 45, 1 to 4) as Boolean You may also define an array of strings but only when Option AllocStr is in effect as it is by default. Dim msg(1 To 4) as String BasicX Compatibility Note In BasicX mode, each array dimension must have at least two elements. Also, arrays of type String are not supported. There is no checking, either at compile time or at run time, for array index underflow or overflow. If you write code that uses an index outside of the defined range of indices, the results are undefined. Defining Subroutines A subroutine is a collection of statements that can be executed by using the subroutine name in a Call statement. The advantage of creating subroutines is that we can think of them as logical blocks instead of thinking about all of the details that are dealt with by the statements within the subroutine.