真的没有办法编写 Fortran 语句,例如
call character_variable_containing_subroutine_name
提供这种功能与 Fortran 等静态类型的编译语言背道而驰。
当然,如果您问过我能否向 Fortran 程序提供一个输入参数,该程序将在运行时确定程序采用的执行路径,那么答案是 当然。我将忽略您的情况的任何复杂性,并假设您想拨打sin、cos 或tan 之一。
首先,将程序的参数文本捕获到字符变量中:
character(len=*) :: user_choice
...
call get_command_argument(1,user_choice)
...
select case (user_choice)
case ('sin')
... do stuff with sin
case ('cos')
... do stuff with cos
case ('tan')
... do stuff with tan
case default
... do whatever
end select
您可以通过使用过程指针使这更复杂。例如,您可以定义:
pointer :: rp
interface
real function rp(inval)
real, intent(in) :: inval
end function rp
end interface
然后将select case 构造的第一个版本替换为:
select case (user_choice)
case ('sin')
rp => sin
case ('cos')
rp => cos
case ('tan')
rp => tan
case default
... do whatever
end select
这可能会简化以后的代码。我想这也可能使它变得更复杂。
请注意,我没有测试任何这些片段,我的语法可能有点不靠谱。