Start Back Next End
  
ZBasic Language Reference
23
ZBasic Microcontrollers
BasicX Compatibility Note
In BasicX mode, UnsignedInteger, UnsignedLong and String constants are
not supported.  Also, constant expressions cannot utilize built-in functions.
Defining Variables
To define a variable at the module level (as opposed to within a subroutine or function, described later)
the syntax is:
{Public | Private | Dim} <name> As <type>
Dim has exactly the same effect as Private, i.e., the variable will only be directly accessible to code
within the module.
Examples
Dim ival as Integer, pulseCount as Byte
Private busy as Boolean
Dim msg as String
The first example above shows two different variables being defined on the same line.
The initial value of a variable depends on its type and how it is defined.  See Section 2.12 for information
on variable initialization.
Defining Arrays of Variables
Variables which hold a single value like those discussed above are called scalar variables.  ZBasic also
supports arrays of variables.  An array of a fundamental type may be defined using the syntax:
{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.
Previous page Top Next page