【发布时间】: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 进行编译。
【问题讨论】: