next up previous contents index
Next: FUNCTION Statement Up: Procedures Previous: Subroutine and Call Statements

RETURN Statement

The RETURN statement   just consists of the keyword

RETURN

Its effect is to stop the procedure executing and to return control, and where appropriate argument and function values, to the calling program unit. The execution of the END statement at the end of the program unit has the exactly the same effect, so that RETURN is superfluous in procedures which have only one entry and one exit point (as all well-designed procedures should). It is, however, sometimes convenient to use RETURN for an emergency exit. Here is a somewhat simple-minded example just to illustrate the point:

 
     REAL FUNCTION HYPOT(X, Y) 
*Computes the hypotenuse of a right-angled triangle. 
      REAL X, Y 
      IF(X .LE. 0.0 .OR. Y .LE. 0.0) THEN 
          WRITE(UNIT=*,FMT=*)'Warning: impossible values' 
          HYPOT = 0.0 
          RETURN 
      END IF 
      HYPOT = SQRT(X**2 + Y**2) 
      END
This function could be used in another program unit like this:
 
       X = HYPOT(12.0, 5.0) 
       Y = HYPOT(0.0, 5.0)
which would assign to X the value of 13.0000 approximately, while the second function call would cause a warning message to be issued and would return a value of zero to Y.

In the external function shown above it would have been perfectly possible to avoid having two exits points by an alternative ending to the procedure, such as:

 
      IF(X .LE. 0.0 .OR. Y .LE. 0.0) THEN 
          WRITE(UNIT=*,FMT=*)'Warning: impossible values' 
          HYPOT = 0.0 
      ELSE  
          HYPOT = SQRT(X**2 + Y**2) 
      END IF 
      END
In more realistic cases, however, the main part of the calculation would be much longer than just one statement and it might then be easier to understand the working if a RETURN statement were to be used than with almost all of the procedure contained within an ELSE-block. A third possibility for emergency exits is to use an unconditional GO TO statement to jump to a label placed on the END statement.



Mario Storti
Wed Nov 4 19:32:56 ART 1998