子程序的名称可以是fortran中的变量吗?

时间:2015-05-18 16:38:12

标签: fortran fortran90 subroutine

我想知道在FORTRAN中是否有类似的东西。当然这个例子没有编译,但我认为你可以理解,

program test
    character(1):: sub

    sub='A'
    call sub

    sub='B'
    call sub
end program

subroutine A
    print*,'OK! A'
end subroutine A

subroutine B
    print*,'OK! B'
end subroutine B

谢谢。

2 个答案:

答案 0 :(得分:2)

您无法通过设置字符变量来实现此目的,但您可以使用过程指针执行此操作。我稍微修改了你的例子以实现它。参见:

program test
 implicit none

 abstract interface
    subroutine no_args
    end subroutine
 end interface

 procedure(no_args), pointer :: sub => null()

 sub => A
 call sub

 sub => B
 call sub

contains

subroutine A
 implicit none
 print *,"OK! A"
end subroutine

subroutine B
 implicit none
 print *,"OK! B"
end subroutine

end program

变化是:

  • 定义过程指针的接口
  • sub声明为该抽象接口的过程指针

然后,您可以将sub分配给不带参数的子例程(因为这就是接口所说的内容),并按照您想象的方式通过sub调用它们。

答案 1 :(得分:0)

最接近的是函数/过程指针,但那将是fortran-2003。基本上,给定输入“A”或“B”,您可以将指针设置为指向subroutine Asubroutine B。更多详情请见How to alias a function name in Fortran