In our last issue, the Good Doctor covered the topic of optional arguments, noting that an explicit interface was required. Since explicit interfaces seem to be a common point of confusion for those new to Fortran 90, (and some not so new), we’ll cover this subject in more detail.
In Fortran terminology, an interface is a declaration of some other procedure that supplies details, including:
- Name of the procedure
- Whether it is a subroutine or function
- If a function, the result type
- Number, names, shapes and types of arguments
- Argument attributes, such as OPTIONAL and INTENT
Prior to Fortran 90, the only kind of interface was implicit, meaning that the compiler assumed that a routine call matched
the actual routine – all you could do was specify the type of a function. The standard required that “the actual arguments … must agree in order, number and type with the corresponding dummy arguments in the dummy argument list of the referenced subroutine.” Not only was this error-prone, but it made it difficult to support desirable features such as optional arguments and array function results.
The explicit interface, introduced with Fortran 90, allows you to tell the compiler many more details about the called routine. This additional information allows a compiler to check for consistency in routine calls and also enables
features such as optional arguments that depend on changes in the way the routine is called. In most cases, an explicit interface consists of an INTERFACE block which contains a copy of the called routine’s declaration. For example:
SUBROUTINE MYSUB (A,B)
REAL, OPTIONAL, INTENT(IN) :: B
END SUBROUTINE MYSUB
An INTERFACE block can go in the declaration section of a program unit, or can be made visible by use-association (in a MODULE that is USEd) or host-association (a program unit that contains the one that needs to see the interface.) An explicit interface also exists, without an INTERFACE block, if the routine is a contained procedure or is a module procedure in the enclosing module or a module that is use-associated (and the module procedure has not been made PRIVATE).
While there are many good reasons why you should always use explicit interfaces, including better error checking and improved run-time performance (avoiding unnecessary copy-in, copy-out code), there are some cases where you are required to have an explicit interface visible. These are:
- If the procedure has any of the following:
- An optional dummy argument
- A dummy argument that is an assumed-shape array, a pointer, or a target
- A result that is array-valued or a pointer (functions only)
- A result whose length is neither assumed nor a constant (character functions only)
- If a reference to the procedure appears as follows:
- With an argument keyword
- As a reference by its generic name
- As a defined assignment (subroutines only)
- In an expression as a defined operator (functions only)
- In a context that requires it to be pure
- If the procedure is elemental
For more information on explicit interfaces, see Chapter 8 of the Language Reference Manual.
In closing, Doctor Fortran prescribes using explicit interfaces throughout your application, ideally with an appropriate INTENT attribute specified for each argument. It may be a bit more typing up front, but it will quickly pay off in smoother development and, possibly, faster execution.
[Revisiting this topic in 2008, my advice has changed. You should avoid writing INTERFACE blocks for Fortran code. Instead, put your subroutines and functions in modules, or make them CONTAINed procedures if they’ll be called from a limited context. This provides the explicit interface without needing to retype the declarations. – Steve]
[I revisited this topic in 2012 – see Doctor Fortran Gets Explicit – Again!]
(From Intel Developer Zone, copied with permission)