Chapter 4: Boe-Bot Navigation · Page 141
Figure 4-5 shows part of a PBASIC program that contains a subroutine call and a
subroutine. The subroutine call is the
GOSUB My_Subroutine command. The actual
subroutine is everything from the
My_Subroutine: label through the RETURN command.
Here’s how it works. When the program gets to the
GOSUB My_Subroutine command,
it looks for the
My_Subroutine: label. As shown by arrow (1), the program jumps to
the
My_Subroutine: label and starts executing commands. The program keeps going
down line by line from the label, so you’ll see the message “Command in subroutine” in
your Debug Terminal.
PAUSE 1000 causes a one second pause. Then, when the
program gets to the
RETURN command, arrow (2) shows how it jumps back to the
command immediately after the
GOSUB command. In this case, it’s a DEBUG command
that displays the message “After subroutine”.
DO
DEBUG "Before subroutine",CR
PAUSE 1000
GOSUB My_Subroutine
DEBUG "After subroutine", CR
PAUSE 1000
LOOP
My_Subroutine:
DEBUG "Command in subroutine", CR
PAUSE 1000
RETURN
Figure 4-5
Subroutine
Basics
Example Program – OneSubroutine.bs2
√ Enter, save, and run OneSubroutine.bs2
' Robotics with the Boe-Bot - OneSubroutine.bs2
' This program demonstrates a simple subroutine call.
' {$STAMP BS2}
' {$PBASIC 2.5}
DEBUG "Before subroutine",CR
PAUSE 1000
GOSUB My_Subroutine
DEBUG "After subroutine", CR
END
My_Subroutine:
1
2