leeyazhou 发表于 2017-10-24 18:52:12

如何使用函数变量

定义两个function, f1, f2,
在subroutine 中 使用名称 f
根据输入不同,可以使f为f1, 或者为f2。
这个怎么实现?

leeyazhou 发表于 2017-10-25 08:39:35

是描述不清楚吗?

kyra 发表于 2017-10-25 09:09:43

可以用 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

pasuka 发表于 2017-10-25 09:41:23

leeyazhou 发表于 2017-10-25 08:39
是描述不清楚吗?

的确是描述不清楚,输入不同这个提法颇为含糊?
究竟怎么个不同呢?输入变量个数不同?输入变量的类型不同?输入变量的数值不同?
还是希望类似C++里面模板和泛型的功能?

leeyazhou 发表于 2017-10-25 10:41:59

kyra 发表于 2017-10-25 09:09
可以用 Procedure Pointers 这个东西(中文应该叫 过程指针)

Module Proc2


谢谢,理解了。

leeyazhou 发表于 2017-10-25 10:42:59

pasuka 发表于 2017-10-25 09:41
的确是描述不清楚,这个提法颇为含糊?
究竟怎么个不同呢?输入变量个数不同?输入变量的类型不同?输入 ...

熟悉C C++,新学fortran。问的是C语言中的函数指针

li913 发表于 2017-10-26 13:13:51

http://fcode.cn/guide-61-1.html

leeyazhou 发表于 2017-10-27 12:36:13

使用Interface可定义函数变量,在gfortran中可用。但是在CVF6.6中不可使用。
interface是新特性,在老版的编译器中,如何实现函数变量定义?

fcode 发表于 2017-10-27 13:07:01

放弃老版编译器,让淘汰的编译器入土为安吧。
页: [1]
查看完整版本: 如何使用函数变量