Lorem ipsum dolor sit amet, consectetur adipiscing elit. Test link
Posts

7.4 Recursion

1 min read
Image result for Recursion c++ 


Recursion is a programming technique that allows the programmer to express operations in terms of themselves.

In C++, this takes the form of a function that calls itself. A useful way to think of recursive functions is to imagine them as a process being performed where one of the instructions is to "repeat the process". This makes it sound very similar to a loop because it repeats the same code, and in some ways it is similar to looping. On the other hand, recursion makes it easier to express ideas in which the result of the recursive call is necessary to complete the task. Of course, it must be possible for the "process" to sometimes be completed without the recursive call.

void CountDown(int nValue)
{
using namespace std;
cout << nValue << endl;
CountDown(nValue-1);
}

int main(void)
{
CountDown(10);
return 0;
}

When CountDown(10) is called, the number 10 is printed, and CountDown(9) is called. CountDown(9) prints 9 and calls CountDown(8). CountDown(8) prints 8 and calls CountDown(7). The sequence of CountDown(n) calling CountDown(n-1) is continually repeated, effectively forming the recursive equivalent of an infinite loop.

This program illustrates the most important point about recursive functions: you must include a termination condition, or they will run “forever” (or until the call stack runs out of memory).
Stopping a recursive function generally involves using an if statement. Here is our function redesigned with a termination condition:

void CountDown(int nValue)
{
using namespace std;
cout << nValue << endl;

// termination condition
if (nValue > 0)
CountDown(nValue-1);
}

int main(void)
{
CountDown(10);
return 0;
}

Now when we run our program, CountDown() will count down to 0 and then stop!

                      8.1 Classes Basics          NEXT >>

You may like these posts

  • C (and by extension C++) comes with a built-in pseudo-random number generator. It is implemented as two separate functions that live in the cstdlib header: srand() sets the initi…
  •   Function Declaration A function declaration tells the compiler about a function name and how to call the function. The actual body of the function can be defined separately…
  • A function is a group of statements that together perform a task. All C programs made up of one or more functions. There must be one and only one main function. You can divide up…
  •   Break statement Break statement is usually used to terminate a case in the switch statement. Break statement in loops to instantly terminates the loop and program contr…
  •   while loop The while loop calculates the expression before every loop. If the expression is true then block of statements is executed, so it will not execute If the condi…
  •   A switch statement is used instead of nested if...else statements. It is multiple branch decision statement of C++. A switch statement tests a variable with list of val…

Post a Comment