1

I am implementing this equation with a Fortran function. geometric series equation

Write call in function (g) consistently returns 6, but when I call function in program (z) output depends, e.g., -2123950080 -529463296 929961984.

Why g and z are not the same? What should I change in function to accomplish geometric series calculation?

function  geomSeries(n)
implicit none
integer :: i, n, g = 0, x = 1, geomSeries

g = 1 + x
do i = 2, n
    g = g + x**i
end do
write (*,*) g
return
end function geomSeries

program geomFunc
implicit none
integer :: n = 5, geomSeries, z

z =  geomSeries(n)
write (*,*) z
end program geomFunc

P.S. Ideally I would like it to be pure function, as I don't see how it produces side effects, but haven't managed to compile it that way (why?).

tRash
  • 131
  • 1
  • 11

0 Answers0