【发布时间】:2014-02-12 23:33:22
【问题描述】:
我已经编写了一组子例程并将它们编译到一个库中。这些子程序基于一些定义的函数(x,y)。目前这被隐藏在库例程中 - 但是我想要能够将任何函数(x,y)传递到这个库中 - 这可能吗?谢谢大家!
【问题讨论】:
-
是的,传递一个过程/函数指针。见stackoverflow.com/questions/8612466/…
标签: function fortran subroutine
我已经编写了一组子例程并将它们编译到一个库中。这些子程序基于一些定义的函数(x,y)。目前这被隐藏在库例程中 - 但是我想要能够将任何函数(x,y)传递到这个库中 - 这可能吗?谢谢大家!
【问题讨论】:
标签: function fortran subroutine
module ExampleFuncs
implicit none
abstract interface
function func (z)
real :: func
real, intent (in) :: z
end function func
end interface
contains
subroutine EvalFunc (aFunc_ptr, x)
procedure (func), pointer :: aFunc_ptr
real, intent (in) :: x
write (*, *) "answer:", aFunc_ptr (x)
end subroutine EvalFunc
function f1 (x)
real :: f1
real, intent (in) :: x
f1 = 2.0 * x
end function f1
function f2 (x)
real :: f2
real, intent (in) :: x
f2 = 3.0 * x**2
end function f2
end module ExampleFuncs
program Func_to_Sub
use ExampleFuncs
implicit none
procedure (func), pointer :: f_ptr => null ()
f_ptr => f1
call EvalFunc (f_ptr, 2.0)
f_ptr => f2
call EvalFunc (f_ptr, 2.0)
stop
end program Func_to_Sub
【讨论】:
used 中,以便您可以使用它来声明函数。