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

  • auto The default class. Automatic variables are local to their block. Their storage space is reclaimed on exit from the block. register If possible, the variable will be…
  • /* This Program prints Hello World on screen */ #include <iostream.h> using namespace std; int main () { cout << "Hello World!"; return 0; } 1 . /* This program ..…
  • Array is a fixed size collection of similar data type items. Arrays are used to store and access group of data of same data type. Arrays can of any data type. Arrays must…
  •   An operator is a symbol. Compiler identifies Operator and performs specific mathematical or logical operation. C provides following operators : # Arithmetic Operators # L…
  • refers to where variables is declared. It can be Inside a function or a block which is called local variables, In the definition of function parameters which is called f…
  •    A variable in C++ is a name for a piece of memory that can be used to store information. There are many types of variables, which determines the size and la…

Post a Comment