【问题标题】:How can I make Fortran recognize my function return type? [duplicate]如何让 Fortran 识别我的函数返回类型? [复制]
【发布时间】:2021-08-29 05:36:29
【问题描述】:

我正在学习 Fortran,我必须编写一个函数来计算一个数字的阶乘。这是我的代码:

program functions
    implicit none

    integer :: fact

    fact = factorial(5)
    print *, fact
    
end program functions

recursive function factorial(n) result(factResult)
    implicit none

    integer :: n
    integer :: factResult

    if (n == 0 .or. n == 1) then
        factResult = 1
    else 
        factResult = n * factorial(n - 1)
    end if

end function factorial

我认为我的代码应该可以工作,但我无法编译,我在调用 factorial(5) 时收到此消息:

Return type mismatch of function 'factorial' at (1)

(UNKNOWN/INTEGER(4)) Function 'factorial' at (1) has no IMPLICIT type

我不知道出了什么问题,似乎没有检测到我的返回类型。在不使用接口或模块的情况下,我该怎么做才能使我的功能正常工作?因为我处于初级水平。

我正在使用 Visual Studio Code,并且正在使用 gfortran 进行编译。

【问题讨论】:

    标签: function fortran


    【解决方案1】:

    您的主程序无法识别factorial() 函数。请注意,即使您的函数与主程序在同一个文件中,但这并不意味着主程序可以自动找到该函数。解决您的问题的一个简单方法是扩展您的主程序以包含该函数,

    program functions
        implicit none
    
        integer :: fact
    
        fact = factorial(5)
        print *, fact
    
    contains
        
        recursive function factorial(n) result(factResult)
            implicit none
        
            integer :: n
            integer :: factResult
        
            if (n == 0 .or. n == 1) then
                factResult = 1
            else 
                factResult = n * factorial(n - 1)
            end if
        
        end function factorial
    
    end program functions
    

    tutorialspoint online Fortran compiler 上测试它,你的程序打印出来了,

    120
    

    解决问题的更好更通用的方法是将您的函数放入一个模块中,然后将 use 模块放入您的主程序中,或任何其他可能需要的地方。

    module factorial_mod
        
        implicit none
    
    contains
        
        recursive function factorial(n) result(factResult)
            implicit none
        
            integer :: n
            integer :: factResult
        
            if (n == 0 .or. n == 1) then
                factResult = 1
            else 
                factResult = n * factorial(n - 1)
            end if
        
        end function factorial
    
    end module factorial_mod
    
    program functions
    
        use factorial_mod, only: factorial
        implicit none
    
        integer :: fact
    
        fact = factorial(5)
        print *, fact
        
    end program functions
    

    另外,请记住,factorial 结果可能会增长得非常快,很容易导致整数溢出。您最好使用 64 位整数来返回结果(在阶乘函数中使用use iso_fortran_env, only: int64),或者甚至更好地返回结果的自然对数(作为实数类型)而不是整数值。

    【讨论】:

    • 谢谢,我会考虑 int64。现在,我将使用您的“包含”建议,因为我对 Fortran 不是很有经验,但是当我了解它时我会使用该模块。我不知道是什么原因导致我的程序找不到我的函数,但我会调查一下。
    • 您的程序“找不到”您的函数,因为您没有告诉程序有关它们的信息。程序只“知道”ProgramEnd Program 语句之间的事情。它什么都不知道,它在同一个文件中是无关。同样,子程序只知道SubroutineEnd Subroutine 之间的事情。如果“东西”在这组行之外,你必须告诉程序/子程序。你没有,所以你有一个错误。要调查的技术词是 scope
    • @IanBush 太好了,现在我明白了,非常感谢。我会继续阅读所有这些内容。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-01
    • 2020-02-21
    • 1970-01-01
    • 2012-08-02
    • 2014-12-08
    • 1970-01-01
    相关资源
    最近更新 更多