如何使用函数变量
定义两个function, f1, f2,在subroutine 中 使用名称 f
根据输入不同,可以使f为f1, 或者为f2。
这个怎么实现? 是描述不清楚吗? 可以用 Procedure Pointers 这个东西(中文应该叫 过程指针)
Module Proc2
Implicit None
interface
Subroutine IF_PROC(x,y)
integer :: x , y
End Subroutine IF_PROC
end interface
contains
Subroutine f1(x,y)
integer :: x , y
write(*,*) x+y
End Subroutine f1
Subroutine f2(x,y)
integer :: x , y
write(*,*) x-y
End Subroutine f2
End Module Proc2
Program Proc_pointer
use Proc2
Implicit None
integer :: x , y , n
Procedure(IF_PROC) , pointer :: proc
read(*,*) n , x , y
if(n==1) then
proc => f1
else
proc => f2
end if
call proc(x,y)
End Program Proc_pointer leeyazhou 发表于 2017-10-25 08:39
是描述不清楚吗?
的确是描述不清楚,输入不同这个提法颇为含糊?
究竟怎么个不同呢?输入变量个数不同?输入变量的类型不同?输入变量的数值不同?
还是希望类似C++里面模板和泛型的功能? kyra 发表于 2017-10-25 09:09
可以用 Procedure Pointers 这个东西(中文应该叫 过程指针)
Module Proc2
谢谢,理解了。 pasuka 发表于 2017-10-25 09:41
的确是描述不清楚,这个提法颇为含糊?
究竟怎么个不同呢?输入变量个数不同?输入变量的类型不同?输入 ...
熟悉C C++,新学fortran。问的是C语言中的函数指针 http://fcode.cn/guide-61-1.html 使用Interface可定义函数变量,在gfortran中可用。但是在CVF6.6中不可使用。
interface是新特性,在老版的编译器中,如何实现函数变量定义? 放弃老版编译器,让淘汰的编译器入土为安吧。
页:
[1]