...
Defining a Subroutine
Panel | |
---|---|
Html bobswift | subroutine name
[script line 1]
...
[script line n]
endsub
</pre>
|
Invoking a Subroutine
Panel |
---|
<pre>
callsub name
</pre>
|
Sequence of Defining / Invoking a Subroutine
...
For example, the following subroutine will function correctly:
Panel |
---|
<pre>
subroutine test
echo "This is subroutine test"
echo "$(_halton)"
endsub
echo "This is main()"
callsub test
</pre>
|
However, this subroutine will fail:
Panel |
---|
<pre>
echo "This is main()"
callsub test
subroutine test
echo "This is subroutine test"
echo "$(_halton)"
endsub
</pre>
|
Nesting / Recursion of Subroutines
...
For example, the following illustrates subroutine nesting:
Panel |
---|
<pre>
subroutine a
echo "Beginning subroutine A"
callsub b
echo "Ending subroutine A"
endsub
subroutine b
echo "Beginning subroutine B"
echo "Ending subroutine B"
endsub
callsub a
</pre>
|
Example
Panel | |
---|---|
Html bobswift | subroutine loop_increment
echo "inside loop_increment: $(LOOP)"
set LOOP=<$(LOOP) + 1>
endsub
echo "Starting Loop:"
set LOOP=0
if <$(LOOP) LT 1>
callsub loop_increment
end
if <$(LOOP) LT 2>
callsub loop_increment
end
if <$(LOOP) EQ 2>
callsub loop_increment
end
echo "Final Value of LOOP: $(LOOP)"
</pre>
|
Output
Panel | |
---|---|
Html bobswift | Starting Loop:
inside loop_increment: 0
inside loop_increment: 1
inside loop_increment: 2
Final Value of LOOP: 3
</pre>
|