是的
call action(mySubX)
提供的动作看起来像
subroutine action(sub)
!either - not recommmended, it is old FORTRAN77 style
external sub
!or - recommended
interface
subroutine sub(aA, aB)
integer,intent(...) :: aA, aB
end subroutine
end interface
! NOT BOTH!!
call sub(argA, argB)
如果action 知道在argA, argB 那里放什么来代表aA, aB。
否则,如果您还想传递参数
call action(mySubX, argA, argB)
subroutine action(sub, argA, argB)
!either - not recommmended, it is old FORTRAN77 style
external sub
!or - recommended
interface
subroutine sub(aA, aB)
integer,intent(...) :: aA, aB
end subroutine
end interface
integer, intent(...) :: argA, argB
call sub(argA, argB)
我不认为在这里使用函数指针是好的,当你有时不得不改变指针(它指向的子程序)的值时它们是好的。普通过程参数在 FORTRAN77 中有效,并且即使现在也继续有效。
所以按照评论中的要求,如果你在一个模块中并且可以从模块中访问具有正确接口的过程(可能在同一个模块中),你可以使用过程语句来获取接口块的棒:
module subs_mod
contains
subroutine example_sub(aA, aB)
integer,intent(...) :: aA, aB
!the real example code
end subroutine
end module
module action_mod
contains
subroutine action(sub)
use subs_mod
procedure(example_sub) :: sub
call sub(argA, argB)
end subroutine
end module
但更有可能的是,您将创建一个抽象接口,而不是一个真正的子例程,您将使用过程语句引用该接口,因此最终一切都将与以前相似:
module action_mod
abstract interface
subroutine sub_interface(aA, aB)
integer,intent(...) :: aA, aB
end subroutine
end interface
contains
subroutine action(sub)
procedure(sub_interface) :: sub
call sub(argA, argB)
end subroutine
end module