473,394 Members | 1,717 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,394 software developers and data experts.

what is recursion in c++.

..plz define it.
Jan 5 '08 #1
20 2952
On Sat, 05 Jan 2008 20:17:17 +0100, at**********@gmail.com
<at**********@gmail.comwrote:
.plz define it.

RTFM !
Google is not dead !
Jan 5 '08 #2
"at**********@gmail.com" <at**********@gmail.comwrote in comp.lang.c++:
.plz define it.
Mainly "recursion" is where a function calls itself. Something like:

long unsigned Factorial(unsigned const input)
{
if (0 == input) return 1;

if (1 == input) return input;

return input * Factorial(input - 1);
}

--
Tomás Ó hÉilidhe
Jan 5 '08 #3
On 2008-01-05 19:17:17 +0000, "at**********@gmail.com"
<at**********@gmail.comsaid:
.plz define it.
http://en.wikipedia.org/wiki/Recursion

Jan 5 '08 #4
On Jan 5, 2:17 pm, "athar.mir...@gmail.com" <athar.mir...@gmail.com>
wrote:
.plz define it.
As the English word suggests: a recursion is simply a function that
calls itself.
Which is neither efficient nor beneficial.
Each call generates a new local stack which eventually all need to be
unwound.
So in C++, its usually replaced with better alternatives.
An example of a preferred alternative would then be a function object
(functor).
Recursive functions have no state (what does that mean?).

To give you an example of recursion:
(that incidentally doesn't do what you'ld expect)

#include <iostream>

void recursion(int n)
{
std::cout << "n = ";
std::cout << n << std::endl;
if(n < 10)
recursion(++n);
}

int main()
{
recursion(0); // prints 11 times
}
Jan 5 '08 #5
On Jan 5, 2:23 pm, David Côme <davidc...@wanadoo.frwrote:
On Sat, 05 Jan 2008 20:17:17 +0100, athar.mir...@gmail.com

<athar.mir...@gmail.comwrote:
.plz define it.

RTFM !
Google is not dead !
And neither is a little respect.
Some come here hoping others do their homework,
others prefer to ask rather than remain clueless.
Some of those know very little English.

So please: if you feel the need to reply with a patronising remark,
consider ignoring them, redirecting them to Wikipedia/Google or offer
them a link to the FAQ.
Jan 5 '08 #6
On Jan 5, 2:19*pm, Salt_Peter <pj_h...@yahoo.comwrote:
On Jan 5, 2:17 pm, "athar.mir...@gmail.com" <athar.mir...@gmail.com>
Each call generates a new local stack which eventually all need to be
unwound.
So in C++, its usually replaced with better alternatives.

I agree. But my old college professors thought it was great.

And personally, I also find it more difficult to debug.
Jan 5 '08 #7
On Sat, 05 Jan 2008 21:41:38 +0100, Salt_Peter <pj*****@yahoo.comwrote:
On Jan 5, 2:23 pm, David Côme <davidc...@wanadoo.frwrote:
>On Sat, 05 Jan 2008 20:17:17 +0100, athar.mir...@gmail.com

<athar.mir...@gmail.comwrote:
.plz define it.

RTFM !
Google is not dead !

And neither is a little respect.
Some come here hoping others do their homework,
others prefer to ask rather than remain clueless.
Some of those know very little English.

So please: if you feel the need to reply with a patronising remark,
consider ignoring them, redirecting them to Wikipedia/Google or offer
them a link to the FAQ.


You could have a basic question because everybody has been noob when he
started programmation.
Howerver before ask usenet,forums... you must make some research.

And recursion on google was the fisrt link so....

Jan 5 '08 #8
Salt_Peter <pj*****@yahoo.comwrote in comp.lang.c++:
As the English word suggests: a recursion is simply a function that
calls itself.
Which is neither efficient nor beneficial.

It's great though for template metaprogramming. For instance:

template<long unsigned x>
struct Factorial {
static long unsigned const val = x * Factorial<x-1>::val;
};

template<>
struct Factorial<1{
static long unsigned const val = val;
};

template<>
struct Factorial<0{
static long unsigned const val = 1;
}
--
Tomás Ó hÉilidhe
Jan 5 '08 #9
Tomás Ó hÉilidhe wrote:
Salt_Peter <pj*****@yahoo.comwrote in comp.lang.c++:
>As the English word suggests: a recursion is simply a function that
calls itself.
Which is neither efficient nor beneficial.


It's great though for template metaprogramming. For instance:

template<long unsigned x>
struct Factorial {
static long unsigned const val = x * Factorial<x-1>::val;
};

template<>
struct Factorial<1{
static long unsigned const val = val;
};

template<>
struct Factorial<0{
static long unsigned const val = 1;
}
recursion [ri-kur'zhen] - (n) see recursion

--
Jim Langston
ta*******@rocketmail.com
Jan 5 '08 #10
On 2008-01-05 15:19:08 -0500, Salt_Peter <pj*****@yahoo.comsaid:
On Jan 5, 2:17 pm, "athar.mir...@gmail.com" <athar.mir...@gmail.com>
wrote:
>.plz define it.

As the English word suggests: a recursion is simply a function that
calls itself.
Which is neither efficient nor beneficial.
Bummer. We'll have to stop using quicksort.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Jan 6 '08 #11
at**********@gmail.com wrote:
.plz define it.

RECURSION: see RECURSION.
Jan 6 '08 #12
Jim Langston wrote:
recursion [ri-kur'zhen] - (n) see recursion
Damn. I posted the exact same definition before I saw your post. GMTA.
Jan 6 '08 #13
In article <m3*****************@newssvr21.news.prodigy.net> ,
no*****@here.dude says...
at**********@gmail.com wrote:
.plz define it.


RECURSION: see RECURSION.
Error stack overflow at 1.

Recursion: if (understood) return; else see Recursion.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jan 6 '08 #14
On 2008-01-05 21:30:42 -0500, red floyd <no*****@here.dudesaid:
at**********@gmail.com wrote:
>.plz define it.


RECURSION: see RECURSION.
That's not recursion. That's just circular reasoning.

The difference is that circular reasoning is not logically sound, but
recursion is. More specifically, a definition using recursion admits
existence and uniqueness of an object under that definition, while
circular reasoning is ambiguous.

For example, the recursion
f(n+1) = f(n) + 1
f(0) = 0
admits the actual function
f(n) = n.

On the other hand, the circular-reasoning "definition"
g(n) = g(n)
admits any function since that definition is vacuously true for any function.

--

-kira

Jan 6 '08 #15
"at**********@gmail.com" <at**********@gmail.comwrote:
.plz define it.
Here is an example of recursion that people don't often think of as
such...

class Foo {
public:
list< Foo* foos;
void bar() {
for_each( foos.begin(), foos.end(), mem_fun( &Foo::bar ) );
}
};

int main() {
Foo f[10];
f[0].foos.push_back( &f[1] );
f[0].foos.push_back( &f[2] );
f[0].foos.push_back( &f[3] );
f[2].foos.push_back( &f[4] );
f[2].foos.push_back( &f[5] );
f[2].foos.push_back( &f[6] );
f[3].foos.push_back( &f[7] );
f[7].foos.push_back( &f[8] );
f[8].foos.push_back( &f[9] );

f[0].bar();
}
Jan 6 '08 #16
On Sat, 05 Jan 2008 20:26:10 -0700, Jerry Coffin wrote:
In article <m3*****************@newssvr21.news.prodigy.net> ,
no*****@here.dude says...
>at**********@gmail.com wrote:
.plz define it.


RECURSION: see RECURSION.

Error stack overflow at 1.

Recursion: if (understood) return; else see Recursion.
Warning: condition always evaluates to false

--
Lionel B
Jan 6 '08 #17
On Jan 5, 9:19 pm, Salt_Peter <pj_h...@yahoo.comwrote:
On Jan 5, 2:17 pm, "athar.mir...@gmail.com"
<athar.mir...@gmail.comwrote:
.plz define it.
As the English word suggests: a recursion is simply a function
that calls itself. Which is neither efficient nor beneficial.
It's beneficial when its beneficial. On most modern
architectures, it's often as efficient as the alternatives. (I
once benchmarked a recursive version of quick sort, and one
which avoided recursion. the recursive version was faster.)
Each call generates a new local stack which eventually all
need to be unwound. So in C++, its usually replaced with
better alternatives. An example of a preferred alternative
would then be a function object (functor).
How is that relevant to recursion. A functional object is still
a function, and can still be recursive or not, according to how
you write it.
Recursive functions have no state (what does that mean?).
I don't know? I have no idea what you're trying to say there.
To give you an example of recursion: (that incidentally
doesn't do what you'ld expect)
#include <iostream>
void recursion(int n)
{
std::cout << "n = ";
std::cout << n << std::endl;
if(n < 10)
recursion(++n);
}
int main()
{
recursion(0); // prints 11 times
}
And your point is?

Any iterative algorithm can be rewritten to use recursion. Some
languages don't even have looping constructions, but in C++,
using recursion to simluate iteration is poor programming
practice. On the other hand, recursion is considerably more
powerful than iteration, and not all recursive algorithms can be
rewritten in terms of pure iteration, unless you introduce a
manually managed stack, which is a lot more work for the
programmer, and typically less efficient in terms of run-time.
How would you do a depth first visit of a tree without
recursion, for example? (Or quick sort, for that matter? The
standard non-recursive iteration requires a user managed stack.)

Note that recursion is typically less efficient in terms of
memory than managing the stack yourself, so you'll normally want
to avoid recursing too deeply.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jan 6 '08 #18
On Jan 6, 6:37*pm, James Kanze <james.ka...@gmail.comwrote:
On Jan 5, 9:19 pm, Salt_Peter <pj_h...@yahoo.comwrote:
On Jan 5, 2:17 pm, "athar.mir...@gmail.com"
<athar.mir...@gmail.comwrote:
.plz define it.
As the English word suggests: a recursion is simply a function
that calls itself. *Which is neither efficient nor beneficial.

It's beneficial when its beneficial. *On most modern
architectures, it's often as efficient as the alternatives. *(I
once benchmarked a recursive version of quick sort, and one
which avoided recursion. *the recursive version was faster.)
Each call generates a new local stack which eventually all
need to be unwound. *So in C++, its usually replaced with
better alternatives. *An example of a preferred alternative
would then be a function object (functor).

How is that relevant to recursion. *A functional object is still
a function, and can still be recursive or not, according to how
you write it.
Recursive functions have no state (what does that mean?).

I don't know? *I have no idea what you're trying to say there.
To give you an example of recursion: (that incidentally
doesn't do what you'ld expect)
#include <iostream>
void recursion(int n)
{
* std::cout << "n = ";
* std::cout << n << std::endl;
* if(n < 10)
* * recursion(++n);
}
int main()
{
* recursion(0); // prints 11 times
}

And your point is?

Any iterative algorithm can be rewritten to use recursion. *Some
languages don't even have looping constructions, but in C++,
using recursion to simluate iteration is poor programming
practice. *On the other hand, recursion is considerably more
powerful than iteration, and not all recursive algorithms can be
rewritten in terms of pure iteration, unless you introduce a
manually managed stack, which is a lot more work for the
programmer, and typically less efficient in terms of run-time.
How would you do a depth first visit of a tree without
recursion, for example? *(Or quick sort, for that matter? *The
standard non-recursive iteration requires a user managed stack.)

Note that recursion is typically less efficient in terms of
memory than managing the stack yourself, so you'll normally want
to avoid recursing too deeply.

--
James Kanze (GABI Software) * * * * * * email:james.ka...@gmail.com
Conseils en informatique orientée objet/
* * * * * * * * * *Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
I need some memory related information about recursion.
That is how it is implemented
Jan 8 '08 #19
ta********@gmail.com wrote:
>
I need some memory related information about recursion.
That is how it is implemented
You answer is found here:

http://www.parashift.com/c++-faq-lit...t.html#faq-5.2
Jan 8 '08 #20
ta********@gmail.com wrote:
[SNIP]
I need some memory related information about recursion.
That is how it is implemented
AFAIK a recursive function isn't different than a non recursive function.
The function just calls itself like it could call any function pushing the
parameters onto the stack, in an implementation that uses a stack.

With enough recursion you can run out of stack space, which is why there's a
practicle limit to the number of times you can recurse.

--
Jim Langston
ta*******@rocketmail.com
Jan 8 '08 #21

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

Similar topics

11
by: Ken | last post by:
Hello, I have a recursive Sierpinski code here. The code is right and every line works fine by itself. I wish for all of them to call the function DrawSierpinski. But in this cae it only calls...
10
by: paulw | last post by:
Hi Please give problems that "HAS TO" to use recursion (recursive calls to itself.) Preferrably real world examples, not knights tour. I'm thinking about eliminating the use of stack... ...
8
by: No bother | last post by:
I have a table with two columns, one named master, the other slave. Each column has a set of numbers. A number in one column can appear in the other. I am trying to see if there is any infinite...
19
by: Kay Schluehr | last post by:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496691
6
by: Andre Kempe | last post by:
hej folks. i have a heap with fixed size and want to determine the depth of a element with given index at compile-time. therefore i wrote some templates. however, when i use template...
6
by: =?Utf-8?B?c2VlbWE=?= | last post by:
1) What will the value of txt be after executing MyFunction? public void Main() { string txt = MyFunction( “12345†); } public string MyFunction( string str ) { if( str.Length == 1 )
184
by: jim | last post by:
In a thread about wrapping .Net applications using Thinstall and Xenocode, it was pointed out that there may be better programming languages/IDEs to use for the purpose of creating standalone,...
30
by: Jeff Bigham | last post by:
So, it appears that Javascript has a recursion limit of about 1000 levels on FF, maybe less/more on other browsers. Should such deep recursion then generally be avoided in Javascript?...
35
by: Muzammil | last post by:
int harmonic(int n) { if (n=1) { return 1; } else { return harmonic(n-1)+1/n; } } can any help me ??
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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.