2

When is it customarily appropriate to use modules in fortran. Ordinarily, one can put subroutines in a file and compile that without problems. So why and on what conditions is better to use a module? What advantages do modules have from just the normal subroutines?

I have used modules before. But at work, the libraries are basically a long list of files that do not use modules. Thus the question.

Dilna
  • 405
  • 1
  • 6
  • Modules are completely different things from subroutines, and module subprograms are different things from external subprograms. Each can be used for certain things and not others. Please be more precise on a question to get reasonable answers. – francescalus Jan 01 '23 at 21:12
  • 4
    Modules offer an excellent way of packaging relevant functionalities and making organized libraries. External subprograms are relics of the past (the era of FORTRAN77). Never use them anymore. They are highly error-prone because the compiler is unable to perform checks. Always use modules. There is no reason since Fortran 90 to implement external subprograms. Put all procedures in modules. The reason you find many external subprograms is primarily historical and the fact that most Fortran programmers are not familiar with the most recent Fortran standards. – Scientist Jan 01 '23 at 21:29
  • 2
    You can find some discussion around this [other question](https://stackoverflow.com/q/65508576/3157076), but there is an awful lot that isn't addressed there. – francescalus Jan 01 '23 at 21:53
  • Do others agree with Scientist approach ? – Dilna Jan 01 '23 at 23:27
  • The advice of Scientist is a reasonable starting point, but is certainly not complete. "Put all procedures in modules." falls down, for example, by the fact that not all procedures (such as those which aren't written in Fortran) can live within a module. – francescalus Jan 01 '23 at 23:53
  • Converting large suites into modules can often lead to mutual dependencies between modules so if you are going to do it, do it in pigeon steps. You don't want to hit multiple mutual dependency problems where some modules require others to be built first. – cup Jan 02 '23 at 07:21
  • I don't agree with the extreme point of view of @Scientist. While I agree that one should tend to use modules whenever possible, I have experienced some cases where life was easier without modules. Also, *"the compiler is unable to perform checks [without modules]"* is wrong: one can achieve this objective by defining separate interfaces and placing them in include files (the C/C++ way, actually)... – PierU Jan 02 '23 at 07:53
  • 3
    IMO The default should be to place all Fortran procedures in a module. There has to be a *very* good reason not to do this – Ian Bush Jan 02 '23 at 10:20
  • It is easy to link the library with the modules to multiple programs. I suppose if it was only one program, then maybe it could just be compiled as a single bunch of code. But I also often use different compiler flags for the library than for the main program, and the library can have some testing for each module. – Holmz Jan 02 '23 at 18:10
  • 1
    If you're defining separate interfaces, I can't see a reason not to put those interfaces in a module. I'd consider that a superior alternative to an include file. – Craig Jan 06 '23 at 20:06

0 Answers0