Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Defining a Subroutine

<pre>
subroutine name
[script line 1]
...
[script line n]
endsub
</pre>
Panel
Html bobswift

Invoking a Subroutine

html-bobswift
Panel
<pre>
callsub name
</pre>

Sequence of Defining / Invoking a Subroutine

...

For example, the following subroutine will function correctly:

html-bobswift
Panel


<pre>
subroutine test
   echo "This is subroutine test"
   echo "$(_halton)"
endsub

echo "This is main()"
callsub test
</pre>



However, this subroutine will fail:

html-bobswift
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:

html-bobswift
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

<pre>
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>


Panel


Html bobswift

Output

<pre>
Starting Loop:
inside loop_increment: 0
inside loop_increment: 1
inside loop_increment: 2
Final Value of LOOP: 3
</pre>


Panel


Html bobswift