Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,398 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,105 people online right now. Registration is fast and FREE... Join Now!




Recursion

 
Reply to this topicStart new topic

Recursion

DeeViLiSh
4 Sep, 2006 - 09:02 AM
Post #1

D.I.C Head
Group Icon

Joined: 25 Jul, 2006
Posts: 175



Thanked: 1 times
Dream Kudos: 575
My Contributions
Just to get things cleared up in my mind :
What is recursion??

I saw like 10 snippets saying it uses recursion but I don't see what's so special about it.
I was wondering if it could be useful to know and perhaps make my programs run better and such.
User is offlineProfile CardPM
+Quote Post

skyhawk133
RE: Recursion
4 Sep, 2006 - 09:08 AM
Post #2

Head DIC Head
Group Icon

Joined: 17 Mar, 2001
Posts: 14,974



Thanked: 48 times
Dream Kudos: 1650
Expert In: Web Development

My Contributions
Have a look at this tutorial, might clear things up a little: http://forums.dreamincode.net/showtopic11809.htm
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Recursion
4 Sep, 2006 - 09:11 AM
Post #3

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
http://en.wikipedia.org/wiki/Recursion

Recursion in programming can be adequately defined as a program or function that is at least partially defined in terms of itself...take the following code:
CODE

int factorial(int);

void main(void) {
    int number;
    
    cout << "Please enter a positive integer: ";
    cin >> number;
    if (number < 0)
        cout << "That is not a positive integer.\n";
    else
        cout << number << " factorial is: " << factorial(number) << endl;
}

int factorial(int number) {
    int temp;

    if(number <= 1) return 1;

    temp = number * factorial(number - 1);
    return temp;
}

You'll note that the function actually calls itself with a new set of parameters until the break condition (in this case, the number 1) is provided. this is a simple example of recursion.

Another coomon example if recursion is th Fibonacci sequence.

Your question is likely to spawn a large thread with various examples and explanations, but the crux of the matter is above.

It is an extremely valuable concept, but it is not always necessary. Your programs to date would probably not benefit from recursion...it does not specifically make them run better, it is simply a methodology to be used where required. An example might be printing or reading a linked list or tree structure where you do not know in advance how many nodes there are.

You may wish to search the forums as well, there are several topics dedicated to recursion.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 03:06AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month