内在函数作为函数参数

时间:2013-10-08 19:53:34

标签: fortran fortran90 argument-passing 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

嗯,到此为止,一切都很棒。当我尝试将此函数与 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

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

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)
--------------------------^

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

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

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

所以我想问一下:

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

如果您对this is the complete source of the modulethis is the source of the main

感兴趣

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

感谢您的时间!

1 个答案:

答案 0 :(得分:4)

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

着眼未来,你可能最好在内在函数周围编写自己的包装函数 - 创建一个简单地调用sin的函数mysin。

相关问题