next up previous contents index
Next: Procedure Independence Up: Procedures Previous: Guidelines

External Procedures

    There are two forms of external procedure, both of which take the form of a complete program unit.

In either form the last statement of the program unit must be an END statement. Any other statements (except PROGRAM or BLOCK DATA statements) may be used within the program unit.

There are two statements provided especially for use in external procedures. The SAVE statement ensures that the values of local variables and arrays are preserved after the procedure returns control to the calling unit: these values will then be available if the procedure is executed subsequently. The RETURN statement may be used to terminate the execution of the procedure and cause an immediate return to the control of the calling unit. Execution of the END statement at the end of the procedure has exactly the same effect. Both of these are described in full later in the section.

Most Fortran systems also allow external procedures to be specified in languages other than Fortran: they can be called in the same way as Fortran procedures but their internal operations are, of course, beyond the scope of this book.

It is best to think of the subroutine as the more general form of procedure; the external function should be regarded as a special case for use when you only need to return a single value to the calling unit.

Here is a simple example of a procedure which converts a time of day in hours, minutes, and seconds into a count of seconds since midnight. Since only one value needs to be returned, the procedure can have the form of an external function. (In fact this is such a simple example that it would have been possible to define it as a statement function.)

 
*TSECS converts hours, minutes, seconds to total seconds. 
       REAL FUNCTION TSECS(NHOURS, MINS, SECS) 
       INTEGER NHOURS, MINS 
       REAL SECS 
       TIME = ((NHOURS * 60) + MINS) * 60 + SECS 
       END
Thus if we use a function reference like TSECS(12,30,0.0) in an expression elsewhere in the program it will convert the time to seconds since midnight (about 45000.0 seconds in this case). The items in parentheses after the function name :
(12,30,0.0)
are known as the actual arguments of the function; these values are transferred to the corresponding dummy arguments
(NHOURS, MINS, SECS)
of the procedure before it is executed. In this example the argument list is used only to transfer information into the function from outside, the function name itself returns the required value to the calling program. In subroutines, however, there is no function name to return information but the arguments can be used for transfers in either direction, or both. The rules permit them to be used in this more general way in functions, but it is a practice best avoided.

The next example performs the inverse conversion to the TSECS function. Since it has to return three values to the calling program unit the functional form is no longer appropriate, and a subroutine will be used instead.

 
*Subroutine HMS converts TIME in seconds into hours, mins,secs. 
      SUBROUTINE HMS(TIME, NHOURS, MINS, SECS) 
      REAL TIME, SECS 
      INTEGER NHOURS, MINS 
      NHOURS = INT(TIME / 3600.0) 
      SECS   = TIME - 3600.0 * NHOURS 
      MINS   = INT(SECS / 60.0) 
      SECS   = TIME - 60.0 * MINS 
      END
In this case the subroutine could be executed by using a statement such as:
 
       CALL HMS(45000.0, NHRS, MINS, SECS) 
       WRITE(UNIT=*, FMT=*) NHRS, MINS, SECS
Here the first argument transfers information into the subroutine, the other three are used to return the values which it calculates. You do not have to specify whether a particular argument is to transfer information in or out (or in both directions), but there are rules about the form of actual argument that you can use in each case. These are explained in full below.


next up previous contents index
Next: Procedure Independence Up: Procedures Previous: Guidelines

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