Why should I use interfaces?

As Alexander Vogt says, interface blocks can be useful for providing generic identifiers and allowing certain compiler checks.

If you’re using interface blocks to create generic identifiers you’re probably doing so within modules, so the main reason to write such blocks when modules aren’t about is for the explicit interfaces that they create in the scope in which they occur. It’s these that allow those compiler checks and the use association with modules no longer happens.

However, there are times when an explicit interface is required, rather than being a nicety: if referencing a procedure with certain features an explicit interface will be required for your code to conform to the Fortran standard. Those features can be found in F2008, 12.4.2.2

A procedure other than a statement function shall have an explicit interface if it is referenced and

  1. a reference to the procedure appears
    (a) with an argument keyword (12.5.2), or
    (b) in a context that requires it to be pure,
  2. the procedure has a dummy argument that
    (a) has the ALLOCATABLE, ASYNCHRONOUS, OPTIONAL, POINTER, TARGET, VALUE, or VOLATILE attribute,
    (b) is an assumed-shape array,
    (c) is a coarray,
    (d) is of a parameterized derived type, or
    (e) is polymorphic,
  3. the procedure has a result that
    (a) is an array,
    (b) is a pointer or is allocatable, or
    (c) has a nonassumed type parameter value that is not a constant expression,
  4. the procedure is elemental, or
  5. the procedure has the BIND attribute.

Beyond those, an implicit interface will be sufficient and the interface block not required. But could still helpful.

In your case, it’s only for the calls between the various Fortran components where interfaces have this impact. So, you don’t always need to write interfaces and some things can work without them.

Leave a Comment