Navigation bar
  Start Previous page
 28 of 206 
Next page End  

ZBasic Language Reference
20
ZX Microcontroller Family
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.
A subroutine may be defined as taking zero or more parameters.  If it has parameters defined, you must
supply a value for each of the parameters when you invoke the subroutine using the Call statement. 
The syntax for defining a subroutine is shown below.
[Public | Private] Sub <name> ( [<parameter-list>] )
[<statements>]
End Sub
If neither Public nor Private is specified, Public is assumed.
The <parameter-list> consists of zero or more parameter specifications, each separated from the
next by a comma.  Note that the parentheses are required even when there are no parameters.  The
parameters given in the subroutine’s definition are called the “formal parameters”.  The parameters that
appear in each invocation of the subroutine are referred to as the “actual parameters”.
The syntax for a formal parameter specification is:
[ByVal | ByRef] <name> As <type>
The <name> element is the name by which the passed parameter is known within the subroutine.  The
a default value for a parameter, a topic that is discussed in more detail in Section 3.18.
The keywords ByVal and ByRef refer to the method by which the parameter is passed to the subroutine. 
The keyword ByVal means that the parameter is passed to the subroutine “by value” while ByRef means
that the parameter is passed “by reference”.  Each of these parameter passing conventions has its own
advantages and disadvantages and the full explanation of the difference between these two parameter
passing methods is given in Section 2.14.  Suffice to say here that the primary difference is whether or
not the called subroutine is able to change the value of the passed parameter from the perspective of the
caller.  Some variable types may be passed by one of the methods but not by the other.  Again, see
Section 2.14 for complete details.  The default passing convention, if neither ByVal nor ByRef is
specified, is ByRef.
To specify a formal parameter that is an array, simply add a set of parentheses following the parameter
name.  For example,
Sub mySub(ByRef data() as Byte)
End Sub
Only one-dimensioned arrays whose lower bound is 1 may be passed as parameters and they must be
passed by reference.  The upper bound is indeterminate – it is the responsibility of the programmer to
ensure that the subroutine does not access elements beyond the upper bound of the passed array. 
Previous page Top Next page