RETURN Statement

Returns to the calling program from a subroutine or external function.

Syntax

Subroutine

RETURN {TO statementLabel}

External Function

RETURN(value)

Syntax Elements

TO statementLabel
Specifies a label that marks the statement to which control is to be transferred. Refer to the section Statement Labels.

value The value to be returned by an external function.

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 statementLabel. If the statement label does not exist, an error message is displayed at compile time.

Requirements

Example 1

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

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

Example 2

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.

Example 3

**************************************
* Subroutine DISTANCE
*
* Computes the distance between two points.
*
SUBROUTINE DISTANCE(X1, Y1, X2, Y2, D)
   DX = X2 – X1
   DY = Y2 – Y1
   D = SQRT(DX * DX + DY * DY)
   RETURN
END
**************************************

This example defines an external subroutine called DISTANCE that accepts five arguments and it computes the distance between two points (defined by the X1/Y1 and X2/Y2 parameters). On return, the result is in the D parameter.

Example 4

**************************************
* Function DISTANCE
*
* Returns the distance between two points.
*
**************************************
FUNCTION DISTANCE(X1, Y1, X2, Y2)
   DX = X2 – X1
   DY = Y2 – Y1
   RETURN(SQRT(DX * DX + DY * DY))
END

This does the same as the previous example, but is written as an external function that returns the result of the calculation.

Go to top button