Proper way to write a module in fortran

Viewed 81

Suppose I have a module

module module_1
end module module_1

And another module which uses the first module.

  • i wrote it in this way (use module in the main section)
module module_2
use module_1

contains
    function func_1
    end function func_1

    subroutine sub_1
    end subroutine sub_1
end module_2
  • or should i write in this way (use module in every function and subroutine)
module module_2

contains
    function func_1
    use module_1
    end function func_1

    subroutine sub_1
    use module_1
    end subroutine sub_1
end module_2
1 Answers

Answering what you should do is quite opinion-based and hence off-topic. Technically, either is possible.

What we can tell you is the difference. If you use module_1 in the whole module, it will also be acessible through host-association in any future function or subroutine you add into module_2. It is also use-associated and available for all type definitions and module variable definitions in module_2. You might want that, you might not want that. The decision is only up to you. I have certainly used either possibility many times.

If you have reasons to restrict the namespace pollution by only using module_1 in the function and the subroutine, just go ahead and do that. But the reasons must be evaluated by you, there is no universal recipe.

Related