Recursive function

I am trying to write a recursive function in fortran that sums numberes 1 to n? I just need help with the function, I understand how to call from the main program...Thanks!
Cynthia JoubiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dr. KlahnPrincipal Software EngineerCommented:
Many (I would venture to say, most) versions of FORTRAN do not support recursion.  Recursion was first supported in FORTRAN 90, and in the real world not in all F90 implementations.  First check the reference manual for the particular FORTRAN compiler to see if recursion is supported.

If recursion is supported, then:

sum(n) = n + sum(n-1)
sum(0) = 0
0
Cynthia JoubiAuthor Commented:
For the version that I am using, the recursion function seems to work.
So when I try to write the recursive function, the Sum(n) is the function or is that the result?
I know that the recursive function calls on itself, but is the Sum(n) the function?

I was going to go about it this way:

....
recursive function Sumofnumbers(n) result(res)
            
      implicit none

      integer :: res, n, i
      
      if (n .eq. 0) then
            res = 0
      else
            do i = 1, n
            res = Sumofnumbers(n) + n
            end do
      endif      
      
end function Sumofnumbers
....

But I don't seem to get the right sum...
0
Dr. KlahnPrincipal Software EngineerCommented:
Consider this pseudo-code example instead, which more clearly demonstrates recursion:


integer function sum(input)
if (input ?= 0) then
  sum = 0
else
  sum = input + sum(input-1)
end if


Note that if this example is fed a negative integer, overflows will occur.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cynthia JoubiAuthor Commented:
Thank you Dr. Klahn! I am going to close this question, but I appreciate the help and I got my recursive code to finally work! Best, Cynthia.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.