473,773 Members | 2,345 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

what is recursion in c++.

..plz define it.
Jan 5 '08
20 3005
On 2008-01-05 15:19:08 -0500, Salt_Peter <pj*****@yahoo. comsaid:
On Jan 5, 2:17 pm, "athar.mir...@g mail.com" <athar.mir...@g mail.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**********@gm ail.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.n et>,
no*****@here.du de says...
at**********@gm ail.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.d udesaid:
at**********@gm ail.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**********@g mail.com" <at**********@g mail.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.n et>,
no*****@here.du de says...
>at**********@gm ail.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...@g mail.com"
<athar.mir...@g mail.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 objektorientier ter Datenverarbeitu ng
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...@gm ail.comwrote:
On Jan 5, 9:19 pm, Salt_Peter <pj_h...@yahoo. comwrote:
On Jan 5, 2:17 pm, "athar.mir...@g mail.com"
<athar.mir...@g mail.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 objektorientier ter Datenverarbeitu ng
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********@gmai l.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

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

Similar topics

11
3905
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 the first recusive function and at soon as n = 4 its stop. How do I go about so that all 8 lines get 4 recusion thanks ken
10
2525
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... Thanks.
8
1829
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 recursion in the table. E.g.: Master Slave 1 2 2 1
19
2286
by: Kay Schluehr | last post by:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496691
6
2938
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 index_properties<unsigned int>, i get a compiler-error, complaining about the template-recursion of __index_properties__. when i add a partially specialized template (the three commented lines) to stop the template-recursion, it works. does anyone how to...
6
1928
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
7120
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, single executable apps. My goal is to create desktop applications for use on Windows XP+ OSs that are distributed as single executables that do not require traditional install packages to run. I would like to use a drag and drop UI development...
30
8307
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? Surprisingly, deep recursion actually isn't that slow for what I'm doing. Programming this task recursively is so much more straightforward to me, but currently I'm forced to use an ugly hack to avoid going over the 1000 level limit. Of course, it could...
35
4741
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
9621
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9914
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8937
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7463
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5355
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3610
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.