Fortran中模块、子程序和函数的正确使用 [英] Correct use of modules, subroutines and functions in Fortran

查看:50
本文介绍了Fortran中模块、子程序和函数的正确使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近在向 Fortran 程序添加函数时了解了接口块.一切正常,但现在我想在接口块中添加第二个功能.

I've recently learnt about interface blocks when adding a function to my Fortran program. Everything works nice and neatly, but now I want to add a second function into the interface block.

这是我的接口块:

interface
    function correctNeighLabel (A,i,j,k)
    integer :: correctNeighLabel
    integer, intent(in) :: i,j,k
    integer,dimension(:,:,:),intent(inout) :: A
    end function

    function correctNeighArray (B,d,e,f)
        character :: correctNeighArray
    integer, intent(in) :: d,e,f
    character, dimension(:,:,:),intent(inout) :: B
    end function
end interface

在我看来,这可能不是最佳选择.

It appears to me that this may not be the best option.

我已经研究了子程序,但我不太确定这是正确的解决方案.我做的比较简单,需要给子程序传递参数,但是我看到的所有子程序都是a)复杂(即对于函数来说太复杂了),以及b)不带参数.它们的行为就像是在操纵变量而不将变量传递给它们.

I've looked into subroutines, but I'm not very confident that it's the right solution. What I'm doing is relatively simple, and I need to pass arguments to the subroutine, but all the subroutines I've seen are a) complicated (i.e. too complicated for a function), and b) don't take arguments. They behave as though they manipulate variables without them being passed to them.

我并没有真正正确地研究模块,但从我所看到的来看,它不是正确的使用方式.

I've not really looked into modules properly, but from what I've seen it's not the right thing to use.

我应该在什么时候使用哪个,我该如何最好地使用它?

Which should I use when, and how do I go about it best?

推荐答案

使用模块总是正确的 ;-)

Modules are always the right thing to use ;-)

如果您有一个非常简单的 F90 程序,您可以在包含"块中包含函数和子例程:

If you have a very simple F90 program you can include functions and subroutines in the 'contains' block:

 program simple
   implicit none
   integer :: x, y
   x = ...
   y = myfunc(x)
 contains
   function myfunc(x) result(y)
     implicit none
     integer, intent(in)  :: x
     integer              :: y
     ...
   end function myfunc
 end program

然后函数/子程序的接口在程序中是已知的,不需要在接口块中定义.

Then the interface of the functions/subroutines will be known in the program and don't need to be defined in an interface block.

对于更复杂的程序,您应该将所有函数/子程序保存在模块中,并在需要时加载它们.所以你也不需要定义接口:

For more complex programs you should keep all functions/subroutines in modules and load them when required. So you don't need to define interfaces, either:

 module mymod
   implicit none
   private
   public :: myfunc
 contains
   function myfunc(x) result(y)
     implicit none
     integer, intent(in)  :: x
     integer              :: y
     ...
   end function myfunc
 end module mymod

 program advanced
   use mymod, only: myfunc
   implicit none
   integer :: x, y
   x = ...
   y = myfunc(x)
 end program advanced

模块和程序可以(实际上应该)在单独的文件中,但模块必须在实际程序之前编译.

The module and the program can (actually should) be in separate files, but the module has to be compiled before the actual program.

这篇关于Fortran中模块、子程序和函数的正确使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆