Search code examples
modulefortranfortran2008fortran2018

In what situation would you use "module procedure" in a submodule when creating separate module procedures?


I understand the general benefits of submodules in Fortran 2008+, but I'm having trouble understanding the benefit of one of the syntax options.

If I want to create separate module procedures (where the body of the procedure is in a submodule) I need to create an interface in the parent module. That interface must contain the subroutine or function identifier and the argument list and intent. This all makes sense.

Then the submodule contains the implementation of the procedure, but there is a choice of syntax: I can either repeat the signature for the subroutine or function, or, I can simply use the name from the interface after "module procedure." I was going to type out an example but the fortran wiki shows it perfectly.

My question is: What is the benefit of using the alternate syntax using "module procedure" instead of "module function" or "module subroutine", i.e. where the procedure type, arguments, and intent are not duplicated in the submodule file? It seems that this just complicates code maintenance, as now two files need to be open to understand the subroutine or function. Am I missing some hidden benefit here (besides saving a copy/paste)?


Solution

  • The benefit comes later in the life of the code. If you have to change the function's signature, you only have to change it in one location.