我的代码看起来像
subroutine sub1(f)
interface
function f(x)
(description of f)
end function f
end interface
(do something with f)
end subroutine sub1
subroutine sub2(f)
interface
function f(x)
(description of f)
end function f
end interface
(do something with f)
end subroutine sub2
但是,两个子例程sub1
和sub2
都使用相同的接口用于虚函数f
。如何使这两个过程共享相同的接口(例如使用模块)?我必须使用程序指针吗?
答案 0 :(得分:3)
您可以在模块中将此类可重复使用的“函数类型”定义为abstract interface
:
module m
implicit none
abstract interface
function der(x,y) result(yDot)
real, intent(in) :: x, y
real :: yDot
end function
end interface
end module
subroutine integrateEuler(derY,x0,xf,y)
use m
real, intent(in) :: x0, xf
real, intent(inout) :: y
procedure(der) :: derY
! code here
end subroutine
subroutine integrateRKF45(derY,x0,xf,y)
use m
real, intent(in) :: x0, xf
real, intent(inout) :: y
procedure(der) :: derY
! code here
end subroutine
不需要使用函数指针,但您可以用相同的方式声明它们:procedure(der), pointer :: funPtr => myFun