2

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?

Alexander Vogt
  • 17,879
  • 13
  • 52
  • 68
Ignacio
  • 7,646
  • 16
  • 60
  • 113
  • 1
    Isn't it possible to call a Fortran routine in a module directly from R by attaching bind("c",name=...)? – roygvib Jul 25 '15 at 21:03
  • I started learning this stuff a couple of days ago. Can you point me to an example? In this question I tried to use a module and failed http://stackoverflow.com/questions/31395435/use-fortran-subroutine-in-r-undefined-symbol/31396583#31396583 Thanks a lot! – Ignacio Jul 25 '15 at 21:06
  • I've had some luck offering C-interoperable procedures (then called by R) within a Fortran module, but that's probably a brand new question if your experimentation doesn't work. But I largely assumed that this question was about R-Fortran-Fortran calls, and the interfaces for that last part of the chain. – francescalus Jul 25 '15 at 21:08
  • all this stuff is very new to me, I don't really know what "attaching bind" means nor how to do it. I created a new question http://stackoverflow.com/questions/31631239/using-a-fortran-module-in-r Thanks for the help! – Ignacio Jul 25 '15 at 21:19
  • My advise: Don't write unnecessary interfaces. I used to add interfaces, because it was something "we ought to do", according to books and people. In retrospective, this caused at least as many program bugs as it prevented. Now I write as few interfaces as possible. From the perspectives of coding as well as performance, simplicity wins in Fortran. – Norbert S Jan 20 '21 at 21:52

3 Answers3

6

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.

Community
  • 1
  • 1
francescalus
  • 30,576
  • 16
  • 61
  • 96
4

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.

Alexander Vogt
  • 17,879
  • 13
  • 52
  • 68
  • Though Fortran interfaces are surely useful, I always think they are also partly dangerous because there is no mechanism for ensuring their correctness... (unless some automatic generation mechanism is used) – roygvib Jul 25 '15 at 21:09
  • @roygvib And sometimes it's this lack of correctness that is "useful": http://stackoverflow.com/a/29683745. – francescalus Jul 25 '15 at 21:11
3

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.

Ian Bush
  • 6,996
  • 1
  • 21
  • 27