albert
albert

Reputation: 1816

What is the difference between use of nested modules and submodules in Fortran?

I have a module named "mainmodule.f08" and two modules "module1.f08" and "module2.f08". In "mainmodule.f08" I have:

MODULE mainmodule
    use module1
    use module2
END MODULE mainmodule

and I can use the procedures in "module1" and "module2" only stating use mainmodule in my programs.

With submodules is possible to do the same thing, but this is much more complex, because I need to redefine on "mainmodule" all procedures in "module1" and "module2" as a interfaces, considering "module1" and "module2" as submodules:

MODULE mainmodule
    INTERFACE

       ! Same subroutine from module1.
       subroutine submod1(x)
          integer, intent(in) :: x
       end subroutine submod1

       ! Same function from module2.
       function funcmod2(x) result(y)
          integer, intent(in) :: x
          integer :: y
       end function funcmod2

    END INTERFACE
END MODULE mainmodule

Whats is the difference between the two modes of use of the modules if both work in the same way?

Upvotes: 1

Views: 515

Answers (1)

francescalus
francescalus

Reputation: 32366

These two approaches do not work in the same way.

With

MODULE mainmodule
    use module1
    use module2
END MODULE mainmodule

the module mainmodule makes available the public entities in the two other modules. These are then exposed as public through use association of mainmodule.

This is not the same thing as with submodules.

You can read about what submodules are with this other question.

The end result to the user of the final module may look to be the same, but the details for developers of the modules are significant.

Upvotes: 1

Related Questions