- #1
hassamu
- 1
- 0
Hi everybody.
I am learning Fortran on my own, and I started doing simple examples like factorials, iterations, and stuff like that.
The problem comes in recursion, I'm trying to run this simple factorial program using recursion, but the outcome is completely wrong.
I asked to an expert in Fortran, but he said that I need to implement a stack pointer in order to keep track of all the values of my function, but I didn't understand him , and he is no more available this time.
Can somebody point out my mistakes or make suggestions about my program?
Thank you
I am learning Fortran on my own, and I started doing simple examples like factorials, iterations, and stuff like that.
The problem comes in recursion, I'm trying to run this simple factorial program using recursion, but the outcome is completely wrong.
I asked to an expert in Fortran, but he said that I need to implement a stack pointer in order to keep track of all the values of my function, but I didn't understand him , and he is no more available this time.
Can somebody point out my mistakes or make suggestions about my program?
Thank you
Code:
recursive function factorial(myNumber) result (temp)
integer temp, myNumber
if(myNumber<=0) then
temp = 1
return
else
temp = myNumber*factorial(myNumber-1)
return
end if
end function factorial
program recursiveFactorial
integer elNumero, resultado
resultado = 1
elNumero = 0
write(*,*) 'Enter a positive integer:'
read(*,*) elNumero
resultado=factorial(elNumero)
write(*,*) 'X!=',resultado
end program recursiveFactorial