Home
CBM
  ASCII-X
  BASIC
    Disk Commands
    Enter RUN mode
    Program Format
    Secret Variables
    Variable Format
    Expressions
    Keywords
      (divide)
      (equal)
      (less)
      (minus)
      (more)
      (multiply)
      (plus)
      (power)
      Abs
      And
      Append
      Asc
      Atn
      Auto
      Backup
      Bank
      Begin
      Bend
      Bload
      Boot
      Box
      Bsave
      Bump
      Catalog
      Char
      Chr
      Circle
      Close
      Clr
      Cmd
      Collect
      Collision
      Color
      Concat
      Cont
      Copy
      Cos
      Data
      Dclear
      Dclose
      Dec
      Def
      Delete
      Dim
      Directory
      Dispose
      Dload
      Do
      Dopen
      Draw
      Ds
      Ds string
      Dsave
      Dverify
      El
      Else
      End
      Envelope
      Er
      Err
      Exit
      Exp
      Fast
      Fetch
      Filter
      Fn
      For
      Fre
      Get
      Get num
      Getkey
      Go
      Gosub
      Goto
      Graphic
      Gshape
      Header
      Help
      Hex
      If
      Input
      Input num
      Instr
      Int
      Joy
      Key
      Left
      Len
      Let
      List
      Load
      Locate
      Log
      Loop
      Mid
      Monitor
      Movspr
      New
      Next
      Not
      Off
      On
      Open
      Or
      Paint
      Peek
      Pen
      Pi
      Play
      Pointer
      Poke
      Pos
      Pot
      Print
      Print num
      Pudef
      Quit
      Rclr
      Rdot
      Read
      Record
      Rem
      Rename
      Renumber
      Restore
      Resume
      Return
      Rgr
      Right
      Rlum
      Rnd
      Rreg
      Rspcolor
      Rsppos
      Rsprite
      Run
      Rwindow
      Save
      Scale
      Scnclr
      Scratch
      Sgn
      Sin
      Sleep
      Slow
      Sound
      Spc
      Sprcolor
      Sprdef
      Sprite
      Sprsav
      Sqr
      Sshape
      St
      Stash
      Step
      Stop
      Str
      Swap
      Sys
      Tab
      Tan
      Tempo
      Then
      Ti
      Ti string
      To
      Trap
      Troff
      Tron
      Until
      Using
      Usr
      Val
      Verify
      Vol
      Wait
      While
      Width
      Window
      Xor
    Syntax
    Tokens
  C128
  D64plus
  Disk
  Escape Codes
  Hardware
  PCxface
  PETSCII
  Pet2asc
Futurama
IBM PC-AT
Contact
Games
Glossary
Hall of fame
Hall of shame
Miscellaneous
Privacy policy
Programming
Twisty puzzles
KeywordAbbreviationToken (hex)Version(s)Classification
RETURNRE{Shift+T}8E1.0 to 7.0Command* 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