Example Subroutine #1
We illustrate subroutines using the flagsToByte subroutine from the V572 receiver control program as our example. However, for simplicity, we leave out the actual code. The code can be studied in the v572 source code.
This sample subroutine call illustrates the use of parameters passed both in and out, as well as local "My" variables. The important parts of the subroutine are highlighted using colors for clarity. In the program body where we wish to invoke the subroutine we simply place our subroutine call with appropriate parameters. A simple subroutine may have no parameters at all, but our example was chosen to show a variety of possibilities.
$AHZ1 = flagsToByte(4,@Zonelist); # populate it with data retrieved from HAL
The left side parameter, $AHZ1 tagged in red, is set to the value passed via the 'Return' function of the subroutine, also tagged in red below.
Immediately to the right of the equal sign we name the subroutine to call, followed by any input parameters in parenthesis. In this example, we pass two parameters, an integer pointer and array.
#######################################################
sub flagsToByte {
# Takes a list of arrays which have a text boolean "TRUE/FALSE" as element[2]
# Plus an integer pointer into that array.
#
# Returns a BINARY byte where each bit reflects the status of the booleans in the
#######################################################
my($ptr,@incomingZoneList)=@_;
Note the use of 'MY' to designate local variables. Variables designated by 'My' are entirely private to the subroutine.
local @x,$z,$c,$byt,$ptr2,$ctr;
Note the use of 'local' to designate local variables. Variables designated by 'local' are private to the subroutine, but are shared with subroutines called by this subroutine. A subroutine called by this subroutine sees these 'local' variables exactly the same as it would similarly named global variables.
(intervening code deleted for simplicity. See actual V572.pl for actual source code.)
$byt = pack("C*",@byt); # Convert ASCII back to Binary byte.
return $byt; # We're done. Return the modified byte.
}
1;
|