RETURN Statement

Transfers control from a subroutine back to the main program.

Syntax

RETURN {TO statement-label}

Syntax Elements

statement-label is the label of the statement to which control is to be transferred. Refer to the section Statement Labels.

Options

RETURN transfers control from a subroutine back to the statement immediately following the GOSUB or CALL statement that called it.

RETURN TO transfers control from the subroutine to the statement with the specified statement-label. If the statement label does not exist, an error message is displayed at compile time.

Requirement

Every subroutine must return to the calling program by using a RETURN or RETURN TO statement, not a GOTO statement. This ensures proper flow control.

Examples

  GOSUB 15
  PRINT "BACK FROM SUBROUTINE"
  .
  .
15 * SUBROUTINE XYZ
  .
  .
  RETURN

Control returns to the PRINT statement following the GOSUB that called the subroutine.

10 GOSUB 50
  PRINT "SUBROUTINE EXECUTION COMPLETE"
  .
  .
50 * SUBROUTINE HERE
  .
  .
  IF ERROR RETURN TO 99
75 RETURN
99 PRINT "ERROR ENCOUNTERED HERE"; STOP

If an error occurs in the subroutine, control transfers to label 99, an error message is displayed and the program terminates.  Otherwise, control is returned to the statement following the GOSUB that called the subroutine.

Go to top button