Why should I use interfaces?

Whenever I program in Fortran I use modules and I don't have to worry about writing interfaces.

Now I'm writing Fortran code to use inside R. The problem is that the subroutines cannot be inside a module so I "have" to write interfaces. If I don't write the interface everything works fine, but smart internet people said I should write the interfaces.

Can someone explain me why? What are the benefit?


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.


The main benefit is that the compiler can perform argument checks. This way, it can warn you about programming errors, which would be very hard to detect otherwise.

Another very nice thing about interfaces is that you can bundle functions. Consider for example the sin function, which is defined for real and complex arguments of different kinds. Using interfaces, you can call the same function for different types of variables.

For some features of modern Fortran such as function pointers, interfaces are (usually) required.


Just a slightly more "philosophical" viewpoint from the above excellent answers which deal with specific uses of interfaces.

You declare all your variables don't you? Presumably you do this to protect yourself against typos, accidental mis- and ab-use of the variable, and to avoid the reader having knowledge of occasionally arcane rules, amongst other reasons. So why don't you want to do the same for all your subprograms? All the above and more apply.