Some More about Recursion

Written by Raza on. Posted in C++


Deviating a little from our series of discussion on Sorting Algorithms, this time and maybe the next one too, we would be plunging into certain other core concepts considered vital and important to good programming. Last time, we carried out the Selection Sort using the recursive technique. As recursion is a tricky and fundamental topic and a very powerful tool as well, we would be having some more discussion on recursion now.


Types of Recursion :

To have a nice little recap, Recursion is a technique when we call a function from within its own body. Recursion has many types which include linear, tail, binary, exponential, nested and mutual recursion depending upon the method and scenario of the function call. Let us pick up the first and last one.

Linear Recursion occurs when we call a function from within its body in a simple manner only once. We shall see its example after some time too. In Mutual Recursion, a function gets called from a function which is called from its body. Got it? Maybe not. Suppose there is a function named A. Function A calls another function B from its body, and from function B, function A gets called again. So it still is a kind of recursion but a little complicated one.


Using Recursion :

I remember telling you last time how to recognize if the problem at hand could be performed through recursion or not. It was, to identify a procedure which is being done again and again. Instead of doing it in a loop, we call that function repeatedly. What we do in fact is we divide the problem into smaller sub-problems until they become easy enough to be solved easily by us. This technique in Programming is known as Divide and Conquer Algorithm; we divide the problem into small pieces and then solve them.

Remember that recursion is not an easy topic and it takes a lot of practice to understand. There are many rules and conventions to follow while applying recursive techniques, but to keep things simple we won’t be taking up any of those details except for one or two things. However, we would demonstrate recursion through an example.


Calculating Factorial :

A factorial of a number ‘n’ is the product of all the positive integers less than and equal to n. It is demonstrated by the mathematical formula,

n ! = n (n-1) (n-2) (n-3) …….. 3.2.1

n ! = n (n-1) (n-2) !

5 ! = 5 * 4 * 3 !

5 ! = 5 * 4 * 3 * 2 * 1


Normal Procedure :

Using normal programming, we solve this problem with the help of a code like this one.

<p>int Calculate_Factorial (int n)<br />
{<br />
&nbsp; &nbsp; &nbsp;int fact = 1 ;<br />
<p>&nbsp; &nbsp; for (int i = n ; i > 0 ; --i)<br />
&nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; fact = fact * i ;<br />
&nbsp; &nbsp; }<br />
<p>&nbsp; &nbsp; &nbsp;return fact ;<br />


We observe in this code that we multiply the original number repeatedly by all the numbers which are less than itself. Instead of using a loop, we can call the function itself again with the parameter as one number less than the original number. This process would continue until 1 is reached. Secondly, we discussed earlier the mathematical base of a factorial. In this method, there is no use of the true concept of calculating a factorial. Using recursion, we would use the mathematical method of n ! = n (n-1) (n-2) !

Now keep in mind that 0! is equal to 1 according to mathematical conventions. So you should place a check for the case when the incoming number is zero. Having said that, keep this thing in mind that the lowest and simplest cases of any particular problem are extremely important in recursion and they may cause complete destruction of the program if not properly checked. 


Recursive Procedure :

Its code using recursion would become something like this.


<p>int Calculate_Factorial (int n)<br />
{<br />
&nbsp; &nbsp; if (n == 0)</p>
<p>&nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; <a style="text-decoration:none;font: inherit;box-shadow: none;outline:none; color:inherit" href="">Apple Cider Vinegar for Skin Tags</a> &nbsp; return 1 ;</p>
<p>&nbsp; &nbsp; }<br />
&nbsp; &nbsp; else</p>
<p>&nbsp; &nbsp; {</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; return n * Calculate_Factorial (n-1) ;</p>
<p>&nbsp; &nbsp; }</p>

The line in the else statement is the most important as the function gets called again when execution reaches that point. It is called in the following way in each of the steps.

5 * 4 ;

4 * 3 ;

3 * 2 ;

2 * 1 ;

1 * 1 ;

This process when the another function gets called upon the existing one is known as call stack. When the last function gets called, the stack begins to unwind


Structural vs Generative :

Finally, some theoretical talk. In this recursive function, we passed (n-1) as a parameter into the function we called from within. So in fact we used the original data passed to the function in the first instance to derive the parameters in the next function calls. This is known as Structural Recursion. On the other hand, if we use the original parameter to obtain an entirely new piece of data, it would have been known as Generative Recursion. 


So thats it for now. Hope to see you next time with some another interesting piece of information. Good Bye!

Tags: , , ,