I'm learning Fortran and I have to write a function that calculates factorial for a number. Here´s my code:
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
I think my code shoud work but I can't compile, I receive this messages when calling factorial(5):
Return type mismatch of function 'factorial' at (1)
(UNKNOWN/INTEGER(4)) Function 'factorial' at (1) has no IMPLICIT type
I don't know what's wrong, it seems than my return type isn't detected. What can I do to make my function work without using intefaces or modules? Because I'm in a basic level.
I'm using Visual Studio Code and I'm compiling with gfortran.