StickOS
DI-159 PLC Hardware Manual
Subroutines
A subroutine is called with the following statement:
gosub
subname
[
expression
, ...]
A subroutine is declared with the following statements:
sub
subname
[
param
, ...]
statements
endsub
The sub can be exited prematurely using the statement:
return
This causes program execution to immediately return to the statements following the
gosub
statement that called the subroutine.
In general, subroutines should be declared out of the normal execution path of the code, and
typically are defined at the end of the program.
Subroutine parameters are essentially variables local to the subroutine which are initialized to
the values of the caller's gosub expressions. Simple variable caller's gosub
expression
's, how-
ever, are passed to sub
param
's by reference, allowing the sub to modify the caller's variables;
all other caller's gosub expressions are passed by value.
Note that to force a variable to be passed by value to a subroutine, simply use a trivial expres-
sion like "var+0" in the gosub statement expression.
Note also that to return a value from a subroutine, pass in a simple variable (by reference) and
have the subroutine modify the corresponding param before it returns.
Any variables dimensioned in a subroutine are local to that subroutine. Local variables hide var-
iables of the same name dimensioned in outer-more scopes. Local variables are automatically
un-dimensioned when the subroutine returns.
Examples
>
10 dim a
>
20 for a = 0 to 9
>
30 gosub sumit a
>
40 next
>
50 end
55
Summary of Contents for DI-159
Page 51: ...StickOS DI 159 PLC Hardware Manual 4 is even _ 51...
Page 76: ...DI 159 Block Diagram 76...
Page 77: ...Dimensional Drawing 77...