Connecting Tech Pros Worldwide Forums | Help | Site Map

what is recursion in c++.

athar.mirchi@gmail.com
Guest
 
Posts: n/a
#1: Jan 5 '08
..plz define it.

=?utf-8?B?RGF2aWQgQ8ODxpLDgsK0bWU=?=
Guest
 
Posts: n/a
#2: Jan 5 '08

re: what is recursion in c++.


On Sat, 05 Jan 2008 20:17:17 +0100, athar.mirchi@gmail.com
<athar.mirchi@gmail.comwrote:
Quote:
.plz define it.

RTFM !
Google is not dead !
Tomás Ó hÉilidhe
Guest
 
Posts: n/a
#3: Jan 5 '08

re: what is recursion in c++.


"athar.mirchi@gmail.com" <athar.mirchi@gmail.comwrote in comp.lang.c++:
Quote:
.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
Mark
Guest
 
Posts: n/a
#4: Jan 5 '08

re: what is recursion in c++.


On 2008-01-05 19:17:17 +0000, "athar.mirchi@gmail.com"
<athar.mirchi@gmail.comsaid:
Quote:
.plz define it.
http://en.wikipedia.org/wiki/Recursion

Salt_Peter
Guest
 
Posts: n/a
#5: Jan 5 '08

re: what is recursion in c++.


On Jan 5, 2:17 pm, "athar.mir...@gmail.com" <athar.mir...@gmail.com>
wrote:
Quote:
.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
}
Salt_Peter
Guest
 
Posts: n/a
#6: Jan 5 '08

re: what is recursion in c++.


On Jan 5, 2:23 pm, David Côme <davidc...@wanadoo.frwrote:
Quote:
On Sat, 05 Jan 2008 20:17:17 +0100, athar.mir...@gmail.com
>
<athar.mir...@gmail.comwrote:
Quote:
.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.


douggunnoe@gmail.com
Guest
 
Posts: n/a
#7: Jan 5 '08

re: what is recursion in c++.


On Jan 5, 2:19*pm, Salt_Peter <pj_h...@yahoo.comwrote:
Quote:
On Jan 5, 2:17 pm, "athar.mir...@gmail.com" <athar.mir...@gmail.com>
Quote:
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.
=?utf-8?Q?David_C=C3=B4me?=
Guest
 
Posts: n/a
#8: Jan 5 '08

re: what is recursion in c++.


On Sat, 05 Jan 2008 21:41:38 +0100, Salt_Peter <pj_hern@yahoo.comwrote:
Quote:
On Jan 5, 2:23 pm, David Côme <davidc...@wanadoo.frwrote:
Quote:
>On Sat, 05 Jan 2008 20:17:17 +0100, athar.mir...@gmail.com
>>
><athar.mir...@gmail.comwrote:
Quote:
.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....

Tomás Ó hÉilidhe
Guest
 
Posts: n/a
#9: Jan 5 '08

re: what is recursion in c++.


Salt_Peter <pj_hern@yahoo.comwrote in comp.lang.c++:
Quote:
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
Jim Langston
Guest
 
Posts: n/a
#10: Jan 5 '08

re: what is recursion in c++.


Tomás Ó hÉilidhe wrote:
Quote:
Salt_Peter <pj_hern@yahoo.comwrote in comp.lang.c++:
>
Quote:
>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
tazmaster@rocketmail.com


Pete Becker
Guest
 
Posts: n/a
#11: Jan 6 '08

re: what is recursion in c++.


On 2008-01-05 15:19:08 -0500, Salt_Peter <pj_hern@yahoo.comsaid:
Quote:
On Jan 5, 2:17 pm, "athar.mir...@gmail.com" <athar.mir...@gmail.com>
wrote:
Quote:
>.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)

red floyd
Guest
 
Posts: n/a
#12: Jan 6 '08

re: what is recursion in c++.


athar.mirchi@gmail.com wrote:
Quote:
.plz define it.

RECURSION: see RECURSION.
red floyd
Guest
 
Posts: n/a
#13: Jan 6 '08

re: what is recursion in c++.


Jim Langston wrote:
Quote:
recursion [ri-kur'zhen] - (n) see recursion
>
Damn. I posted the exact same definition before I saw your post. GMTA.
Jerry Coffin
Guest
 
Posts: n/a
#14: Jan 6 '08

re: what is recursion in c++.


In article <m3Xfj.33592$JD.4170@newssvr21.news.prodigy.net> ,
no.spam@here.dude says...
Quote:
athar.mirchi@gmail.com wrote:
Quote:
.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.
Kira Yamato
Guest
 
Posts: n/a
#15: Jan 6 '08

re: what is recursion in c++.


On 2008-01-05 21:30:42 -0500, red floyd <no.spam@here.dudesaid:
Quote:
athar.mirchi@gmail.com wrote:
Quote:
>.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

Daniel T.
Guest
 
Posts: n/a
#16: Jan 6 '08

re: what is recursion in c++.


"athar.mirchi@gmail.com" <athar.mirchi@gmail.comwrote:
Quote:
.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();
}
Lionel B
Guest
 
Posts: n/a
#17: Jan 6 '08

re: what is recursion in c++.


On Sat, 05 Jan 2008 20:26:10 -0700, Jerry Coffin wrote:
Quote:
In article <m3Xfj.33592$JD.4170@newssvr21.news.prodigy.net> ,
no.spam@here.dude says...
Quote:
>athar.mirchi@gmail.com wrote:
Quote:
.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
James Kanze
Guest
 
Posts: n/a
#18: Jan 6 '08

re: what is recursion in c++.


On Jan 5, 9:19 pm, Salt_Peter <pj_h...@yahoo.comwrote:
Quote:
On Jan 5, 2:17 pm, "athar.mir...@gmail.com"
<athar.mir...@gmail.comwrote:
Quote:
Quote:
.plz define it.
Quote:
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.)
Quote:
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.
Quote:
Recursive functions have no state (what does that mean?).
I don't know? I have no idea what you're trying to say there.
Quote:
To give you an example of recursion: (that incidentally
doesn't do what you'ld expect)
Quote:
#include <iostream>
Quote:
void recursion(int n)
{
std::cout << "n = ";
std::cout << n << std::endl;
if(n < 10)
recursion(++n);
}
Quote:
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.kanze@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
talk2saber@gmail.com
Guest
 
Posts: n/a
#19: Jan 8 '08

re: what is recursion in c++.


On Jan 6, 6:37*pm, James Kanze <james.ka...@gmail.comwrote:
Quote:
On Jan 5, 9:19 pm, Salt_Peter <pj_h...@yahoo.comwrote:
>
Quote:
On Jan 5, 2:17 pm, "athar.mir...@gmail.com"
<athar.mir...@gmail.comwrote:
Quote:
.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.)
>
Quote:
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.
>
Quote:
Recursive functions have no state (what does that mean?).
>
I don't know? *I have no idea what you're trying to say there.
>
Quote:
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
red floyd
Guest
 
Posts: n/a
#20: Jan 8 '08

re: what is recursion in c++.


talk2saber@gmail.com wrote:
Quote:
>
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
Jim Langston
Guest
 
Posts: n/a
#21: Jan 8 '08

re: what is recursion in c++.


talk2saber@gmail.com wrote:
[SNIP]
Quote:
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
tazmaster@rocketmail.com


Closed Thread


Similar C / C++ bytes