我有一个子例程(最小示例)
subroutine treatfunction(f,input,output)
external, real::f
real, intent(in):: input
real, intent(out):: output
output = f(input) + f(1.0) ! i.e. f has only one argument
end subroutine
Run Code Online (Sandbox Code Playgroud)
和带有两个参数的函数
real function fun(x,a)
real,intent(in)::x,a
Run Code Online (Sandbox Code Playgroud)
现在,对于a
运行时的给定值,我想传递fun
给treatfunction
。所以理想情况下,我想称呼类似
call treatfunction(fun(:,a=a0), input=myinput, output=myoutput)
Run Code Online (Sandbox Code Playgroud)
使用Fortran2003功能gfortran-5
支持进行此操作的最优雅的方法是什么?
当然,我可以在其中插入一个可选的哑元参数a
,treatfunction
并在子例程的主体中f
使用f(x)
或f(x,a)
视情况present(a)
而定。但是更改子例程并不理想。
在Fortran 2008中,您可以将内部函数作为参数传递,而gfortran支持它。
subroutine calling()
a0 = ...
call treatfunction(wrapper, input=myinput, output=myoutput)
contains
real function wrapper(x)
real, intent(in) :: x
wrapper = fun(x,a0)
end function
end subroutine
Run Code Online (Sandbox Code Playgroud)
顺便说一句,我要远离external
邪恶,使用接口块。