问题描述
我最近在向Fortran程序添加函数时了解了有关tinterface块的知识。一切都很好,很整齐,但现在我想在接口块中添加第二个函数。
这是我的接口块:
$ p $
interface
函数correctNeighLabel(A,i,j,k)
integer :: correctNeighLabel
整数,意图(in): :i,j,k
integer,dimension(:,:,:),intent(inout):: A
结束函数
函数correctNeighArray(B,d,e ,f)
character :: correctNeighArray
整数,意图(in):: d,e,f
字符,维度(:,:,:),intent(inout):: B
end function
end interface
在我看来,这可能不是最好的选择。
我研究过子程序,但我不确定这是否是正确的解决方案。我所做的相对简单,我需要将参数传递给子例程,但是我所看到的所有子例程都是复杂的(即对于函数来说太复杂),并且b)不需要参数,它们表现得好像他们在不传递给它们的情况下操纵变量一样。
我没有正确地查看模块,但从我看到的不是正确的东西
我应该在什么时候使用,以及如何最好地使用它?
如果你有一个非常简单的F90程序,你可以在函数和子程序中加入函数和子程序'contains'block:
程序简单
隐式无
integer :: x,y
x = ...
y = myfunc(x)
包含
函数myfunc(x)结果(y)
隐式无
整数,意图(in):: x
integer :: y
...
结束函数myfunc
结束程序
然后函数/子程序在程序中是已知的,不需要在接口块中定义。
对于更复杂的程序,您应该将所有函数/子程序保存在模块中并在需要时加载它们。所以你不需要定义接口:
module mymod
implicit none
private
public :: myfunc
包含
函数myfunc(x)结果(y)
隐式无
整数,意图(in):: x
整数: :y
...
结束函数myfunc
结束模块mymod
程序高级
使用mymod,仅:myfunc
隐式无
integer :: x,y
x = ...
y = myfunc(x)
结束程序高级
模块和程序可以(实际上应该)在单独的文件中,但模块必须在实际程序之前编译。
I've recently learnt about tinterface 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.
Here is my 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, this may not be the best option.
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 ;-)
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中的模块,子例程和函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!