RETURN

RETURN

Top  Previous  Next

 

The RETURN statement returns from an internal subroutine entered by GOSUB or an external subroutine entered by CALL.

 

 

Format

 

RETURN {expr}

RETURN TO label{:}

RETURN FROM PROGRAM

 

where

 

labelis a label in the same program or subroutine as the RETURN statement.

 

expris the value to be returned from a user written function.

 

 

The RETURN statement returns from the most recent GOSUB or CALL statement. Thus the same RETURN statement could leave either an internal or catalogued subroutine.

 

The RETURN expr form of the RETURN statement is only valid in a FUNCTION and returns expr as the result of the function. If the function returns to its caller using a simple RETURN statement with no expr, a null string is returned.

 

The optional TO label clause causes return from a GOSUB to continue execution at the given label rather than at the statement following the GOSUB. This clause is ignored when returning from a CALL. Excessive use of RETURN TO can lead to programs that are extremely difficult to maintain.

 

Sometimes a subroutine needs to return to the calling routine but it is not known how many internal subroutines may be active (e.g. in error paths). The standard way to achieve this in all multivalue database products is with a statement of the form

ERROR.LABEL:  RETURN TO ERROR.LABEL

This slightly strange construct will cause all internal subroutines to return to the RETURN statement and then return to the calling program. This is different from STOP which would also terminate the current sentence.

 

QMBasic provides a rather more readable (and marginally faster) method by using

RETURN FROM PROGRAM

 

 

 

Examples

 

SUBROUTINE PRINT.REPORT(ID)

...statements...

RETURN

END

 

This skeleton subroutine performs its task and then returns to its caller.

 

 

FUNCTION MATMAX(MAT A)

  MAX = A(1)

  N = INMAT(A)

  FOR I = 1 TO N

     IF A(I) > MAX THEN MAX = A(I)

  NEXT I

 

  RETURN MAX

END

 

This function scans a one dimensional matrix and passes back the value of the largest element.

 

 
See also:

CALL, GOSUB