如何将具有多个参数的函数传递给期望只有一个参数的函数的子例程?

Hor*_*sch 3 fortran gfortran

我有一个子例程(最小示例)

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运行时的给定值,我想传递funtreatfunction。所以理想情况下,我想称呼类似

call treatfunction(fun(:,a=a0), input=myinput, output=myoutput)
Run Code Online (Sandbox Code Playgroud)

使用Fortran2003功能gfortran-5支持进行此操作的最优雅的方法是什么?

当然,我可以在其中插入一个可选的哑元参数atreatfunction并在子例程的主体中f使用f(x)f(x,a)视情况present(a)而定。但是更改子例程并不理想。

Vla*_*r F 5

在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邪恶,使用接口块。