内部函数作为函数参数

Alf*_*ago 4 fortran argument-passing fortran90 fortran95

嗯,这就是我今天的问题......

我正在编写一个模块过程,它有一个函数作为参数。这个模块看起来像这样:

module Integ
    implicit none
     <variables declaration>
contains
    function Integral(a,b,f) result(res)
        real, intent(in)     ::a, b
        real                 ::res

        interface
            pure function f(x)
                real, intent(in) :: x
                real             :: f
            endfunction
        endinterface


     <more code of function Integral>

    endfunction Integral

endmodule Integ
Run Code Online (Sandbox Code Playgroud)

那么,到这里为止,一切都很好。当我尝试将此函数与Fortran 内在函数一起使用时,问题就会出现。即,在这段代码中:

program main

use Integ

implicit none

real   ::res,a,b

a=3.0; b=4.0

res=Integral(a,b,sin)  !<- This line does not work

!res=Integral(a,b,sen) !<- This line does work

contains
    function sen(x)
        real, intent(in)   :: x
        real               :: sen

        sen=sin(x)
    endfunction

endprogram
Run Code Online (Sandbox Code Playgroud)

第一行不起作用,给出错误信息:

main.f90(17): error #6404: This name does not have a type, and must have an explicit type.   [SIN]
r=Int1DMonteCarlo(0.0,1.0,sin,10000)
--------------------------^

main.f90(17): error #6637: This actual argument must be the name of an external user function or the name of an intrinsic function.   [SIN]
r=Int1DMonteCarlo(0.0,1.0,sin,10000)
--------------------------^
Run Code Online (Sandbox Code Playgroud)

但是第二行(在 snipplet 中注释)确实如此。

这些错误对我来说非常迷惑,因为sin它是 Fortran 内在函数(与错误 2 相矛盾的东西),因此在每个范围内都是明确的(与错误 1 ​​相矛盾的东西)。

显然我做错了什么,但我不知道是什么。

所以我想问一下:

  • 可以使用内部函数作为实际参数调用模块过程吗?
  • 除了在过程中声明接口之外,我还丢失了一些东西吗?

如果你有兴趣,这是模块的完整源代码这是主要的来源

对不起,如果我问了一个愚蠢的问题。我想我正在按照我现在正在阅读的书(Metcalf,Fortran V:II 的数值食谱)告诉我的方式做事。

感谢您的时间!

Ian*_*anH 5

在主程序中使用内在语句来声明实际参数sin是内在参数。这个要求在 Fortran 标准的内在属性的描述中有详细说明。

着眼于未来,您最好围绕内在函数编写自己的包装函数——创建一个简单地调用 sin 的函数 mysin。

  • 我也看到了 - 但我不明白为什么。留意那些比我知识渊博的人的回复 [这里](http://software.intel.com/en-us/forums/topic/476356)。 (2认同)