RETURN

Statement: Transfers control from a subprogram to the calling program unit.

Syntax

RETURN [expr]

expr
Is a scalar expression that is converted to an integer value if necessary.

The expr is only allowed in subroutines; it indicates an alternate return. (An alternate return is an obsolescent feature in Fortran 95 and Fortran 90.)

Rules and Behavior

When a RETURN statement is executed in a function subprogram, control is transferred to the referencing statement in the calling program unit.

When a RETURN statement is executed in a subroutine subprogram, control is transferred to the first executable statement following the CALL statement that invoked the subroutine, or to the alternate return (if one is specified).

Compatibility

CONSOLE STANDARD GRAPHICS QUICKWIN GRAPHICS WINDOWS DLL LIB

See Also: CALL, CASE

Examples

The following shows how alternate returns can be used in a subroutine:

   CALL CHECK(A, B, *10, *20, C)
   ...
10 ...
20 ...
   SUBROUTINE CHECK(X, Y, *, *, C)
   ...
50   IF (X) 60, 70, 80
60   RETURN
70   RETURN 1
80   RETURN 2
   END

The value of X determines the return, as follows:

Note that an asterisk (*) specifies the alternate return. An ampersand (&) can also specify an alternate return in a CALL statement, but not in a subroutine's dummy argument list.

The following shows another example:

     SUBROUTINE Loop
     CHARACTER in
 10  READ (*, '(A)') in
     IF (in .EQ. 'Y') RETURN
     GOTO 10
 !   RETURN implied by the following statement:
     END

 !The following example demonstrates alternate returns:
     CALL AltRet (i, *10, *20, *30)
     WRITE (*, *) 'normal return'
     GOTO 40
 10  WRITE (*, *) 'I = 10'
     GOTO 40
 20  WRITE (*, *) 'I = 20'
     GOTO 40
 30  WRITE (*, *) 'I = 30'
 40  CONTINUE
     END
     SUBROUTINE AltRet (i, *, *, *)
     IF (i .EQ. 10) RETURN 1
     IF (i .EQ. 20) RETURN 2
     IF (i .EQ. 30) RETURN 3
     END

In this example, RETURN 1 specifies the list's first alternate-return label, which is a symbol for the actual argument *10 in the CALL statement. RETURN 2 specifies the second alternate-return label, and RETURN 3 specifies the third alternate-return label.