Are recursive functions analogous to loops…??

Here I will be discussing about the recursive functions specially with respect to C programming language. Recursive function by definition is nothing but a function calling itself in the program. Recursive function can be regarded as the one of the most logical accessories or tools. This recursive function can be analogous to the loops. That is all those programs that are written using loops (may be the ‘while’ loop, or the ‘do while’ loop or the ‘for’ loop) can be successfully written using this concept ofRecursive function.Now lets study recursive function and I will tell you some generalized rules that I have found out which will help you to code the recursive functions in a better way. Lets code finding a factorial by both normal programming and by recursive functioning method.#include<stdio.h>
int a,i,fact=1;
printf(“nEnter the integer whose factorial is to be found:”);
printf(“nThe factorial of %d is %d.”,a,fact);
}Here we will get the process that takes place is ‘fact’ which is initialized to 1 is multiplied by the integers from 1 toacontinuously with an increment of 1 so as to find factorial. Now in recursive function we need to call the function that multiplies variablefactwith the numbers from 1 toato find factorial. Program goes like this:#include<stdio.h>
int factorial(int x)

return x*=factorial(x-1);
return 1;
int a;
printf(“nEnter the number whose factorial is to be found:”);
printf(“nThe factorial of %d is %d”,a,factorial(a));
}Now just observe, the loop which was used in earlier program had iterations in it that computed the factorial and now the recursive function has condition inside it that call the function as many times as there are iterations in the previous program. The recursive function also has the(x-1)term that acts analogous to the change in variableiof the loop.That is if factorial of 5 is to be found out then, 5 is sent tofactorialfunction as a formal parameter and there 5 is multiplied with a number one less than this and also calls function itself, i.e, factorial .
Now x=4 calls x=3, then x=3 calls x=2 and so on. This is calculated as 5×4, then in next function call, 20×3=60, then 60×2=120 and lastly 120×1=120 is computed and function is terminated by printing the factorial of 5 as 120.
Now, the generalized rules for recursive function :The termination must be properly known or else it will lead to the infinite calling of function.Only the variables that are ought to change during the execution of the function must be initialized with some value inside the recursive function (occasionally).Those variables that are not changing in every iteration or in every call of the function must either be declared globally or must be declared locally and sent as parameters. But declaring them as global variables is a better way of programming.Having a counter variable for having a control on the number of function calls is better way, if you are finding it hard to control the flow and execution with the logical conditions inside the function.With all this hints, now you will be able to write all the loop driven program using aRECURSIVE FUNCTION.

News Reporter

Leave a Reply

Your email address will not be published. Required fields are marked *