在 fortran 中编写模块的正确方法

Han*_*nzo 3 fortran module

假设我有一个模块

module module_1
end module module_1
Run Code Online (Sandbox Code Playgroud)

另一个模块使用第一个模块。

  • 我是这样写的(在主要部分使用模块)
module module_2
use module_1

contains
    function func_1
    end function func_1

    subroutine sub_1
    end subroutine sub_1
end module_2
Run Code Online (Sandbox Code Playgroud)
  • 或者我应该这样写(在每个函数和子程序中使用模块)
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
Run Code Online (Sandbox Code Playgroud)

Vla*_*r F 5

回答你应该做的事情是非常基于意见的,因此是题外话。从技术上讲,两者都是可能的。

我们可以告诉你的是区别。如果您module_1在整个模块中使用,它也可以通过主机关联在您添加到module_2. 它也是与使用相关的,可用于module_2. 你可能想要那个,你可能不想要那个。决定仅由您决定。我当然多次使用过这两种可能性。

如果您有理由仅module_1在函数和子例程中使用来限制命名空间污染,请继续这样做。但原因必须由你评估,没有万能的秘方。