Fortran interface usage
by mostlyharmless from LinuxQuestions.org on (#56PQV)
So: this is a bit of a philosophical or style question perhaps, or perhaps I have no clue!
From what I understand, in modern Fortran, or at least since F95, calling subroutines now either requires an explicit INTERFACE block in the calling program, or the subroutine has to be in a module with a corresponding USE statement in the calling program. Not having either doesn't seem to lead to compiler errors*, but it can lead to run time errors. (my personal recent experience and reading see for example http://www.cs.rpi.edu/~szymansk/OOF90/bugs.html) From what I read, that helps the compiler find and report problems with argument passing errors. *Funny thing that the compiler (gfortran) didn't flag me when I wasn't initially using INTERFACES. Maybe I needed additional warning flags.
At any rate, my question is two fold: is there a preferred style, explicit INTERFACE or MODULE/USE?
For debugging purposes, it's definitely helpful to have subroutines in separate files, not all collected in a module, particularly if they are general purpose subroutines and don't necessarily group together naturally with others. Obviously some subroutines work great in modules together, and if the modules don't get huge, that seems like a nice approach. Of course that means you have to debug everything in the module together, or develop them separately first, then stick them in the module afterwards.
On the other hand, having to put in in INTERFACE statements, which are essentially copies of the first several lines of the subroutine, in every program and subprogram that calls the subroutine, leads to a lot of extra duplicated text in a lot of different places. Isn't that potentially a code development and maintenance issue? What if I have to change the subprogram's interface? Then I have to search through every file everywhere to change the INTERFACE blocks. A USE statement is at least a little easier to manage, not unlike a COMMON block, which are now deprecated.
Or am I supposed to put all the explicit INTERFACE blocks, needed or not, in one INCLUDE file and then put that in all my files that use any of the subprograms in the INCLUDE FILE? That's a little bit like a COMMON block too, but perhaps easier to maintain, and there would be a lot of extra INTERFACES added unnecessarily and I'm not sure that would increase readability.
For the philosophical part, which is probably above my head, and you are free to disagree, I hope politely:
If I were writing C++ stuff/graphics libraries or something, I'd understand the need for an API that doesn't change unless absolutely necessary, and I suppose that corresponds to the INTERFACE blocks, which I suppose are like an API. But Fortran is about FormulaTranslation, not API development, so it all seems a bit too C++-ish. I'm not writing an OS or a compiler, I'm just trying to crunch some numbers. Or maybe I'm just too much of an engineer/number cruncher/neanderthal and not enough of a programmer. Does anyone know what the Fortran modernizers were thinking? I was kind of busy in the 90's-00's and am just catching up now. Were they all C++ programmers (not that there's anything wrong with that :) )? Or am I missing something?


From what I understand, in modern Fortran, or at least since F95, calling subroutines now either requires an explicit INTERFACE block in the calling program, or the subroutine has to be in a module with a corresponding USE statement in the calling program. Not having either doesn't seem to lead to compiler errors*, but it can lead to run time errors. (my personal recent experience and reading see for example http://www.cs.rpi.edu/~szymansk/OOF90/bugs.html) From what I read, that helps the compiler find and report problems with argument passing errors. *Funny thing that the compiler (gfortran) didn't flag me when I wasn't initially using INTERFACES. Maybe I needed additional warning flags.
At any rate, my question is two fold: is there a preferred style, explicit INTERFACE or MODULE/USE?
For debugging purposes, it's definitely helpful to have subroutines in separate files, not all collected in a module, particularly if they are general purpose subroutines and don't necessarily group together naturally with others. Obviously some subroutines work great in modules together, and if the modules don't get huge, that seems like a nice approach. Of course that means you have to debug everything in the module together, or develop them separately first, then stick them in the module afterwards.
On the other hand, having to put in in INTERFACE statements, which are essentially copies of the first several lines of the subroutine, in every program and subprogram that calls the subroutine, leads to a lot of extra duplicated text in a lot of different places. Isn't that potentially a code development and maintenance issue? What if I have to change the subprogram's interface? Then I have to search through every file everywhere to change the INTERFACE blocks. A USE statement is at least a little easier to manage, not unlike a COMMON block, which are now deprecated.
Or am I supposed to put all the explicit INTERFACE blocks, needed or not, in one INCLUDE file and then put that in all my files that use any of the subprograms in the INCLUDE FILE? That's a little bit like a COMMON block too, but perhaps easier to maintain, and there would be a lot of extra INTERFACES added unnecessarily and I'm not sure that would increase readability.
For the philosophical part, which is probably above my head, and you are free to disagree, I hope politely:
If I were writing C++ stuff/graphics libraries or something, I'd understand the need for an API that doesn't change unless absolutely necessary, and I suppose that corresponds to the INTERFACE blocks, which I suppose are like an API. But Fortran is about FormulaTranslation, not API development, so it all seems a bit too C++-ish. I'm not writing an OS or a compiler, I'm just trying to crunch some numbers. Or maybe I'm just too much of an engineer/number cruncher/neanderthal and not enough of a programmer. Does anyone know what the Fortran modernizers were thinking? I was kind of busy in the 90's-00's and am just catching up now. Were they all C++ programmers (not that there's anything wrong with that :) )? Or am I missing something?