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 USE KIND_VALS:ONLY long IMPLICIT NONE INTEGER, INTENT(IN) :: n ! in interface REAL(long), INTENT(IN) :: k,x ! in interface REAL(long), INTENT(OUT) :: sum ! in interface REAL(long) :: 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 ) USE KIND_VALS:ONLY long INTEGER :: n REAL(long), INTENT(IN) :: k,x REAL(long), INTENT(OUT) :: 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). The USE statement is necessary so that the meaning of long can be established. 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.
Return to corresponding overview page
Interfaces are only ever needed for EXTERNAL procedures.
Return to corresponding overview page