# 5.9 RETURN Statement

The RETURN statement transfers control from a subprogram to the program that called the subprogram. You can only use RETURN in a subprogram unit.

The RETURN statement takes the following form:

    RETURN [i]

i
  Is an optional integer constant or expression (such as 2 or I+J) that is converted to an integer value if necessary.

Rules and Behavior

The optional argument i indicates an alternate return from the subprogram. It can be specified only in subroutine subprograms, not in function subprograms. The value of i specifies that the ith alternate return in the actual argument list is to be taken (see the second example that follows in this section).

When a RETURN statement is executed in a function subprogram, control is returned to the calling program at the statement that contains the function reference (see Chapter 6). When a RETURN statement is executed in a subroutine, control is returned either to the first executable statement following the CALL statement that initiated the subroutine, or to the statement label that was specified as the ith alternate return in the CALL argument list.

Examples

In the first example, control is returned to the calling program at the first executable statement following the CALL CONVRT statement.

    SUBROUTINE CONVRT(N,ALPH,DATA,PRNT,K)

    INTEGER ALPH(*),  DATA(*), PRNT(*)

    IF (N .GE. 10) THEN

        DATA(K+2) = N-(N/10)*N

        N = N/10

        DATA(K+1) = N

        PRNT(K+2) = ALPH(DATA(K+2)+1)

        PRNT(K+1) = ALPH(DATA(K+1)+1)

    ELSE

        PRNT(K+2) = ALPH(N+1)

    END IF

    RETURN

    END

The second example shows how alternate returns can be included in a subroutine.

         SUBROUTINE CHECK(X,Y,*,*,C)

          . . .

    50   IF (Z)  60,70,80

    60   RETURN

    70   RETURN 1

    80   RETURN 2

         END

Depending on the computed value of Z, one of the following returns occur:

- If Z is less than zero, a normal return occurs and the calling program continues at the first executable statement following CALL CHECK.

- If Z equals zero, the first alternate return (RETURN 1) occurs.

- If Z is greater than zero, the second alternate return (RETURN 2) occurs.

Control returns to the statement specified as the first or second alternate return argument in the CALL statement argument list; for example, the CALL statement might take the following form:

    CALL CHECK(A,B,*10,*20,C)

In this case, RETURN 1 transfers control to statement label 10 and RETURN 2 transfers control to statement label 20.

If a subroutine includes an alternate return specifying a value less than 1 or greater than the number of alternate return arguments, control returns to the next executable statement after the CALL statement (alternate returns are ignored). You should therefore ensure that the value of i is within the range of alternate return arguments.

Previous Page Next Page Table of Contents