473,387 Members | 1,455 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Recusrion

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
Jul 23 '05 #1
6 1168
Ken wrote:
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
Jul 23 '05 #2
Ken wrote:
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

Jul 23 '05 #3

"Jonathan Mcdougall" <jo***************@gmail.com> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com...
Ken wrote:
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


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
Jul 23 '05 #4
On Sun, 17 Jul 2005 10:48:49 +1000, benben wrote:

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.
----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Jul 23 '05 #5
> 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
Jul 23 '05 #6
On Sun, 17 Jul 2005 16:40:54 +1000, benben wrote:

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.
----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Narendra C. Tulpule | last post by:
Hi, is there any way to allow recusrion in compile()? Something like: src_code = 'def fact(n):\n\tif n <= 1:\n\t\treturn 1\n\telse:' + \ '\n\t\treturn n * fact(n-1)\n\nprint fact(12)\n' cobj =...
3
by: dado | last post by:
I'm implementing a "self-tutorial" into my app, and wondering if there's a way of moving a mouse cursor on command? probably using sys,os modules?
7
by: Alan Searle | last post by:
I would like to run an MS-DOS command from Access (VBA) in order to read a directory tree (into an external file). The command works OK from the command (DOS) window (in XP) but I can't seem to...
17
by: Sobin | last post by:
Hi, I am new to programming...which language should I study to get a hot job. is it C# or C++...? Thanks In advance Sobin
8
by: Pivot_Tables | last post by:
Hi, I have created a recursive SQL Query in DB2 and it works fine until some point in the tree where the data gets into infinite loop. Below are some sample data from my relationship table. ...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.