BS1 icon BS2 icon BS2e icon BS2sx icon BS2p icon BS2pe icon BS2px icon

RETURN Example




Syntax: RETURN


Return from a subroutine, assuming there was a previous GOSUB or ON...GOSUB executed.

Quick Facts

  BS1 All BS2 Models
Related Commands



Maximum number of RETURNs per program Unlimited. However, the number of GOSUBs is limited. See GOSUB for more information.


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 GOSUBcommand for more information.

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)

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.

  GOSUB Hello
  DEBUG "How are you?", CR

  DEBUG "Hello my friend.", CR

Watch Out For Subroutines That Your Program Can "Fall Into"

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.

Go to PBASIC Language home page

Open Getting Started with Stamps in Class

Open Connection Troubleshooting

BASIC Stamp Help Version 2.5.4

Copyright © Parallax Inc.