Commodore‎ > ‎BASIC‎ > ‎Keywords‎ > ‎

RETURN

Keyword Abbreviation Token (hex) Version(s) Classification
RETURN RE{Shift+T} 8E 1.0 to 7.0 Command* and Statement

 
Syntax  
RETURN
 
 
 
Purpose  
Program flow control; exit a subroutine.

 
 
Remarks  
RETURN searches the BASIC statck for the most recent GOSUB entry.  Should it be found, the location of the next BASIC statement is set from the value stored on the stack, and the stack entry (along with any entries that were skipped to find the GOSUB entry) are removed from the stack.  If no GOSUB entry is found on the stack, a RETURN WITHOUT GOSUB ERROR should be generated (there seems to be a bug in some versions that will crash the interpreter instead).
 
Unfortunately, no parameters are allowed.  This makes exiting through multiple levels difficult.  BASIC 4.7 (only) has the DISPOSE command which can remove an entry from the BASIC stack.  In other versions, a kludgy method is to use another command that places entries on the BASIC stack (such as FOR or DO) before invoking a GOSUB.  Then if the RETURN needs to escape multiple levels, a corresponding (NEXT or LOOP) can be used which will discard all the GOSUB entries on the stack in order to get back to initial FOR/DO statement.  If that sounds confusing, then now you know why I call it a kludge!
 
Example 1:
NEW

READY.
10 W$="": GOSUB 100: U$ = N$        : REM get user's name
20 PRINT "HELLO "; U$
30 W$=" PET'S" : GOSUB 100: P$ = N$ : REM get pet's name
40 PRINT "IS " P$ " A DOG"; : INPUT N$
50 IF LEFT$(N$,1) = "Y" THEN PRINT "I LIKE DOGS!"
60 END
100 N$="" : PRINT "WHAT'S YOUR"; W$; " NAME";
110 INPUT N$: IF LEN(N$)<1 OR LEN(N$) > 20 THEN GOTO 100
120 RETURN
RUN
WHAT'S YOUR NAME? HYDRO (user input)
HELLO HYDRO
WHAT'S YOUR PET'S NAME? NONE (user input)
IS NONE A DOG? NO (user input)

READY.
 
RETURN is normally only used as statement (i.e., inside a program).  Attempting to use it as command (in direct mode) will usually generate the RETURN WITHOUT GOSUB ERROR (or crash the system).  However, if the program was stopped while inside a subroutine (with either the STOP statement or the STOP key) then RETURN will search for and remove the GOSUB entry stored on the stack (as described above), but it fails to actually set the location for the next statement to be executed and also fails to switch the interpreter into RUN Mode.  It seems that BASIC fails to update its own secret variable on some machines.  It actually works fine in BASIC v2, but fails on BASIC v3.5 or later.
 
Example 2 (Plus/4 or C128):
NEW

READY.
10 GOSUB 100
20 PRINT "MAIN PROG"
30 END
100 PRINT "SUBROUTINE"
110 STOP
120 RETURN

RETURN

?RETURN WITHOUT GOSUB ERROR
READY.
RUN
SUBROUTINE


BREAK IN 110
READY.
RETURN  note there is no error message this time
    MAIN PROG should have printed, but BASIC failed to set Run Mode!
READY.
CONT    alright enter Run Mode ourself

?RETURN WITHOUT GOSUB ERROR IN 120 
READY.  BASIC forgot to set the next statement when it removed the GOSUB entry!
RUN
SUBROUTINE


BREAK IN 110
READY.
CONT
MAIN PROG

READY.
 
Example 2A (C64):
NEW

READY.
10 GOSUB 100
20 PRINT "MAIN PROG"
30 END
100 PRINT "SUBROUTINE"
110 STOP
120 RETURN

RETURN

?RETURN WITHOUT GOSUB ERROR   this is expected, no GOSUBs are on the stack
READY.
RUN
SUBROUTINE


BREAK IN 110
READY.
RETURN
MAIN PROG

READY.  note there is no error on this machine
  
 
 
Compare With  
 
 
Contrast With  
 
See Also  

© H2Obsession, 2014
Comments