next up previous contents
Next: Interface Example Up: Program Units Previous: Implicit SAVE Attribute

 

Procedure Interfaces

Fortran 90 has introduced a new feature whereby it is possible, often essential and wholly desirable to provide an explicit interface for an external procedure. Such an interface provides the compiler with all the information it needs to allow it to it make consistency checks and ensure that enough information is communicated to procedures at run-time.

Consider the following procedure,

    SUBROUTINE expsum( n, k, x, sum )    ! in interface
     IMPLICIT NONE
     INTEGER, INTENT(IN) :: n            ! in interface
     REAL, INTENT(IN)  :: k,x            ! in interface
     REAL, INTENT(OUT)  :: sum           ! in interface
     REAL :: cool_time
     sum = 0.0
     DO i = 1, n
      sum = sum + exp(-i*k*x)
     END DO
    END SUBROUTINE expsum                 ! in interface

The explicit INTERFACE for this routine is,

    INTERFACE
     SUBROUTINE expsum( n, k, x, sum )
      INTEGER :: n
      REAL    :: k,x
      REAL    :: sum
     END SUBROUTINE expsum
    END INTERFACE

An interface declaration, which is initiated with the INTERFACE statement and terminated by END INTERFACE, gives the characteristics (attributes) of both the dummy arguments (for example, the name, type, kind and rankgif) and the procedure (for example, name, class and type for functions). An interface cannot be used for a procedure specified in an EXTERNAL statement (and vice-versa).

The declaration can be thought of as being the whole procedure without the local declarations (for example, cool_time,) and executable code! If this interface is included in the declarations part of a program unit which calls expsum, then the interface is said to be explicit. Clearly, an interface declaration must match the procedure that it refers to.

It is is generally a good idea to make all interfaces explicit.

An interface is only relevant for external procedures; the interface to an internal procedure is always visible as it is already contained within the host. Interfaces to intrinsic procedures are also explicit within the language.

 An interface only contains:

Interfaces are only ever needed for EXTERNAL procedures.




next up previous contents
Next: Interface Example Up: Program Units Previous: Implicit SAVE Attribute

Adam Marshall ©University of Liverpool, 1996
Fri Dec 6 15:03:35 GMT 1996
Not for commercial use.