GOSUB - BASIC Stamp Command Reference
Page 130
•
BASIC Stamp Programming Manual 2.0b
•
www.parallaxinc.com
Try the example below:
GOSUB Hello
DEBUG "How are you?"
END
Hello:
DEBUG "Hello my friend.", CR
RETURN
The above code 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.
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,
it would see the RETURN again (although it didn't GOSUB to that routine
this time) and because there wasn't a previous place to return to, the
BASIC Stamp will start the entire program over again. This would cause
an endless loop. The important thing to remember here is to always make
sure your program doesn't allow itself to "fall into" a subroutine.
Only a limited number of GOSUBs are allowed per program (as shown in
Table 5.14), but they may be nested only four levels deep. In other words,
the subroutine that’s the destination of a GOSUB can contain a GOSUB to
another subroutine, and so on, to a maximum depth (total number of
GOSUBS before the first RETURN) of four. Any deeper, and the program
will "forget" its way back to the starting point (the instruction following
the very first GOSUB).
When GOSUBS are nested, each RETURN takes the program back to the
instruction after the most-recent GOSUB. As is mentioned above, if the
BASIC Stamp encounters a RETURN without a previous GOSUB, the
entire program starts over from the beginning. Take care to avoid these
phenomena.
W
ATCH OUT FOR SUBROUTINES THAT
YOUR PROGRAM CAN
"
FALL INTO
."
GOSUB
LIMITATIONS
.
1
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).
Содержание BASIC Stamp 2e
Страница 1: ...BASIC Stamp Programming Manual Version 2 0c...
Страница 30: ...Introduction to the BASIC Stamps Page 28 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 34: ...Quick Start Guide Page 32 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 90: ...BUTTON BASIC Stamp Command Reference Page 88 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 118: ...END BASIC Stamp Command Reference Page 116 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 128: ...FREQOUT BASIC Stamp Command Reference Page 126 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 196: ...NAP BASIC Stamp Command Reference Page 194 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 206: ...OWIN BASIC Stamp Command Reference Page 204 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 214: ...OWOUT BASIC Stamp Command Reference Page 212 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 216: ...PAUSE BASIC Stamp Command Reference Page 214 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 226: ...POLLMODE BASIC Stamp Command Reference Page 224 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 232: ...POLLOUT BASIC Stamp Command Reference Page 230 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 236: ...POLLRUN BASIC Stamp Command Reference Page 234 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 240: ...POLLWAIT BASIC Stamp Command Reference Page 238 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 262: ...RCTIME BASIC Stamp Command Reference Page 260 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 274: ...RUN BASIC Stamp Command Reference Page 272 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 310: ...SEROUT BASIC Stamp Command Reference Page 308 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 324: ...STOP BASIC Stamp Command Reference Page 322 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 340: ...ASCII Chart Page 338 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 342: ...Reserved Words Page 340 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 346: ...Conversion Formatters Page 344 BASIC Stamp Programming Manual 2 0b www parallaxinc com...