what is recursion in c++.

T

Tomás Ó hÉilidhe

.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);
}
 
S

Salt_Peter

.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
}
 
S

Salt_Peter

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.
 
D

douggunnoe

On Jan 5, 2:17 pm, "(e-mail address removed)" <[email protected]>
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.
 
D

David Côme

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....
 
T

Tomás Ó hÉilidhe

Salt_Peter said:
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;
}
 
J

Jim Langston

Tomás Ó hÉilidhe said:
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
 
P

Pete Becker

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.
 
K

Kira Yamato

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.
 
D

Daniel T.

.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();
}
 
J

James Kanze

On Jan 5, 2:17 pm, "(e-mail address removed)"
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.
 
T

talk2saber

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.)


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.


I don't know?  I have no idea what you're trying to say there.


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:[email protected]
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
 

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
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top