Navigation bar
  Start Previous page
 22 of 156 
Next page End 17 18 19 20 21 22 23 24 25 26 27  

16
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
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)
' alternately blink the LEDs
Do
' turn on the red LED
Call Blink(redLed)
' turn on the green LED
Call Blink(grnLed)
Loop
End Sub
Private Sub Blink(ByVal pin as Byte)
' turn on the LED connected to the specified pin for one half second
Call PutPin(pin, zxOutputLow)
Call Delay(0.5)
Call PutPin(pin, zxOutputHigh)
End Sub
In this program, we have factored out the code that turns an LED on and off into a subroutine named
Blink().  In the definition of Blink(), pin is called the formal parameter.  In Main() where Blink()
is invoked, the parameters redLed and grnLed are the actual parameters.
By factoring out the code that was common to blinking the two LEDs we have simplified the program. 
The details of how an LED is blinked are encapsulated in the definition of Blink().  No longer does the
Main() subroutine need to know how to blink an LED; it just calls the subroutine to handle all of the
details of blinking an LED connected to a specific pin and provides the necessary data for Blink() to do
the work.  In this case, all that is needed is a pin number.
If we wished to do so, we could add another parameter to the blink subroutine to specify how long we
want the LED illuminated.
Previous page Top Next page