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 rank
) 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.
Interfaces are only ever needed for EXTERNAL procedures.