BASIC stamp manual v2.2

5: BASIC Stamp Command Reference – RETURN
BASIC Stamp Syntax and Reference Manual 2.2 www.parallax.com Page 375
RETURN
BS1 BS2 BS2e BS2sx BS2p BS2pe BS2px
RETURN
Function
Return from a subroutine, assuming there was a previous GOSUB
executed.
Quick Facts
Table 5.89: RETURN Quick Facts.
BS1 BS2 Models
Related
Commands
GOSUB GOSUB and ON
Maximum Number
of RETURNS
per Program
Unlimited. However, the number of GOSUBs are limited.
See GOSUB for more information.
Explanation
RETURN sends the program back to the address (instruction) immediately
following the most recent GOSUB. If RETURN is executed without a prior
GOSUB, the BASIC Stamp will return to the first executable line of the
program; usually resulting in a logical bug in the code. See the GOSUB
command for more information.
The example below will start out by GOSUB'ing to the section of code
beginning with the label Hello. It will print "Hello, my friend!" on the
screen then RETURN to the line after the GOSUB… which prints "How are
you?" and ENDs.
Main:
GOSUB Hello
DEBUG "How are you?"
END
Hello:
DEBUG "Hello, my friend!", CR
RETURN
There's another interesting lesson here; what would happen if we removed
the END command from this example? Since the BASIC Stamp reads the
code from left to right / top to bottom (like the English language) once it
had returned to and run the "How are you?" line, it would naturally "fall
into" the Hello routine again. Additionally, at the end of the Hello routine,
WATCH OUT FOR SUBROUTINES THAT
YOUR PROGRAM CAN
"FALL INTO."
1
A
ll
2
NOTE: On the BS1, a RETURN
without a GOSUB will return the
program to the last GOSUB (or will end
the program if no GOSUB was
executed).
1