Recusrion

K

Ken

Hello,

I have this recusrion code below I know how the looping works, but where I
get confused is with the return 1, How does it know to send the accumulated
value and not just returning the number 1 with any n input.

int recursion(int n)
{
if (n == 0) return 1;
else return n * recursion(n-1);
};


-Ken
 
V

Victor Bazarov

Ken said:
I have this recusrion code below I know how the looping works, but
where I get confused is with the return 1, How does it know to send
the accumulated value and not just returning the number 1 with any n
input.
int recursion(int n)
{
if (n == 0) return 1;
else return n * recursion(n-1);
};

Recursion cannot be explained, only shown. Take the 'else' part and
expand the contents. Pretend you're a compiler and you need to inline
the code in this function. (I know that recursive functions cannot be
inlined during compilation, just pretend you can and do it as if it is
during run-time)

V
 
J

Jonathan Mcdougall

Ken said:
Hello,

I have this recusrion code below I know how the looping works, but where I
get confused is with the return 1, How does it know to send the accumulated
value and not just returning the number 1 with any n input.

int recursion(int n)
{
if (n == 0) return 1;
else return n * recursion(n-1);
};

What's important is the 'n*recursion(n-1)' part, which does the actual
accumulation.

int main()
{
recursion(3);
}

does:

recursion(n=3)
return 3*recursion(2)
recursion(n=2)
return 2*recursion(1)
recursion(n=1)
return 1
return 2*1
return 3*2*1
return 6

Do it on a piece of paper if that's not clear enough. It's quite simple
once you understand it.


Jonathan
 
B

benben

Jonathan Mcdougall said:
What's important is the 'n*recursion(n-1)' part, which does the actual
accumulation.

int main()
{
recursion(3);
}

does:

recursion(n=3)
return 3*recursion(2)
recursion(n=2)
return 2*recursion(1)
recursion(n=1)
return 1
return 2*1
return 3*2*1
return 6

Do it on a piece of paper if that's not clear enough. It's quite simple
once you understand it.


Jonathan

That said, it is always good to be cautious with recursions. Where both
iteration and recursion can be used, prefer iteration over recursion.

And that, of course, excludes educational or intentional exercises...

Ben
 
C

Chris Barts

That said, it is always good to be cautious with recursions. Where both
iteration and recursion can be used, prefer iteration over recursion.

Uh, no. Do whatever is most natural given the algorithm you want to
express and let the compiler optimize everything into efficient machine
code. Handicapping yourself out of efficiency concerns is misguided
unless you have actually /profiled/ and /know/ it is unacceptably
inefficient to do things the natural way.
 
B

benben

Uh, no. Do whatever is most natural given the algorithm you want to
express and let the compiler optimize everything into efficient machine
code. Handicapping yourself out of efficiency concerns is misguided
unless you have actually /profiled/ and /know/ it is unacceptably
inefficient to do things the natural way.

Exactly! And how many times you find recursion more natural than iteration?
And how many times you find recursive code easier to maintain?

Regards,
Ben
 
C

Chris Barts

Exactly! And how many times you find recursion more natural than iteration?
And how many times you find recursive code easier to maintain?

I find it easier quite often. When dealing with trees, for example,
recursion is really the only option that doesn't bend my brain. And
mathematical operations are often defined in terms of recursion instead of
iteration.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top