by hyperrail · 5y ago · 0 comments
I haven't written Fortran in a while, but I was pretty sure that for illustrative examples like this, you could dispense with the entire MODULE declaration, the use of END FUNCTION Fibonacci instead of just END, and the usually-optional :: separator between the variable's type and name.
Something like this? Again, no recent experience:
implicit none
recursive function fibonacci(n) result (fib)
integer n
integer fib
if (n < 2) then
fib = n
else
fib = fibonacci(n - 1) + fibonacci(n - 2)
endif
end
(The IMPLICIT NONE has to stay because of the now-regrettable Fortran convention that without it, the type of a variable is determined by the first character of the name (n would be integer because variables starting with m, n, i, etc. are integer, while fib would be floating point).)No comments yet.