29 of 208
ZBasic Language Reference 21 ZX Microcontroller Family 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 <type> element is one of the fundamental types listed in the table in Section 2.2. You may also specify 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. Often, programmers will include a parameter that specifies the upper bound so that the code may safely operate with different sizes of arrays. Sub mySub(ByRef data() as Byte, ByVal count as Byte) End Sub Lastly, the <statements> element in the subroutine definition represents zero or more valid ZBasic statements that are described in Section 2.5 of this manual. Example Private Const redLED as Byte = 25 ' define the pin for the red LED Private Const grnLED as Byte = 26 ' define the pin for the green LED Public Sub Main() ' configure the pins to be outputs, LEDs off Call PutPin(redLed, zxOutputHigh) Call PutPin(grnLed, zxOutputHigh)