In this tutorial we will learn Recursion Function in C programming Language.
Recursion Function: The recursion is the process in c programming language which allows to repeat the certain section of code in a program.
Thus, in the C programming languages the program allows the user to call the self function inside the same function, it is known as a recursive call in that function .
when ever the function calls same copy of itself then it is the actual recursion . It is useful when we do want to work on a smaller problem.
All the problems can not be solved by using recursion in C programming Langguage. It is mainly useful and benificial for the similar types of subtask.For e.g. one can apply recursion on sorting, searching, and traversal problems.
But one important thing we need to keep in our mind that the iterative or repeatative(looping) solutions are very efficient than the recursion. why It is efficient because the function call is almost overhead all the time.
The recursion in a C program continues further until and unless it meets some condition to ultimately prevent it.
The recursion function continues its process infinite times. To stop or prevent the continues execution of the function we should mention the exit condition.
Thus, if we want to prevent recursion in our programming code we have to use the if…else statement or some similar type of acondition or approch where one of the branches will makes a recursive call while the other one doesn’t.
Recursion Function in C Language
Working of Recursion Function in C Programming Language.
In C programming language the recursion can be made by calling function to itself. The process of calling function to itself is called recursion.
The recursive function performs some specific task by dividing it in to subtask.
Let understand the recursion function from the given diagam.
we have declared and defined the function recur_fun().
The main() function calls the recur_fun().
The control transfers to the recur_fun() and start executing statements inside recur_fun() body.
Inside recur_fun() we have again called the recur_fun() which calls the same function again and this process continues infinite times.
There should be a termination condition that we need to define in the function to stop infinite execution of recursion function. Some specific subtasks should satisfy this termination condition. Due to this, the recursive execution of function stops and we get the final result.
Simple Recursion Function example in C Language.
#include<stdio.h>
void recurse_fun()
{
printf("\n Welcome to sankalan Data Technologies.");
recurse_fun(); // calling the recurse_fun() itself
}
int main()
{
printf("\n Call the recurse-function...");
recurse_fun(); // calling recurse_fun() from main() function
return(0);
}
Output:
Call the recurse-function...
Welcome to sankalan Data Technologies.
Welcome to sankalan Data Technologies.
.
.
.
.
Explanation:
1.The main() program function calls the recurse_fun().The control transfers to the recur_fun() body and start executing the statements.
The printf() function is executed and gives the output "Welcome to sankalan Data Technologies" .
2. Again the recurse_fun() has been called inside the the function body , which again call the same function and the process continues infinite times giving the output "Welcome to sankalan Data Technologies".
3. This recursive execution may crash the system so that we should put the exit condiotion.
C program to find Factorial of a Number .
The following example given is a simple implementation of recursion functions in C Programming that calculates the factorial of a number.
#include <stdio.h>
int fact_n(int n)
{
if(n==1 || n==0)
{
return 1;
}
else
{
return n*fact_n(n-1);
}
}
int main()
{
int x;
printf("\n Enter any Number");
scanf("%d",&x);
printf("\n The Factorial of %d!=%d",x,fact_n(x));
return(0);
}
Output:
Enter any Number:
3.
The Factorial of 3! is 6.
Explanation:In the given program above we have declared and defined the function fact_n() which calculates the factorial of any number entered by user.
Let us understand how the program calculates and process the factorial step by step.
step 1: int x;
printf("\n Enter any Number");
scanf("%d",&x);
The variable x has been declared as integer which one has used to store any integer number whose factorial we is to actually find.
The given printf() prompts us the Message "Enter any Number:" we have entered the number 3.
The scanf("%d",&x); function store the entered integer number 3 at the address of x or in the x variable.
Step 2: The next Statement printf("\n The Factorial of %d!=%d",x,fact_n(x)); executes gives the final resul.
The Factorial of 3!=6.
Let Understand how does Recursion Function fact_n(x) calculates the factorial.
step 2.1: printf("\n The Factorial of %d!=%d",x,fact_n(x));
inside the printf() function the fact_n(x) function calls the
int fact_n(int n) function where the value of x=3 is passed to fact_n(int n) so n is 3 .
step 2.2:The if(n==1 ||n==0) condition does not match then the control transfer to the statements inside else the return n*fact(n-1) statement which one calculate ,call and return the result. here the statement becomes return 3*fact(2);
step 2.3: the statement from above again invokes the return n*fact(n-1).
now here value of n is 2.
The statement return 2*fact(1) which is again call the fact() function.Inside the fact() function the condiion is checked i.e if(n==1 || n==0) which is true. then the return 1 statement returns the value 1 to the function called from step 2.3 .i.e return 2*1.
The return 2*1 returns 2 to the step number 2.2.
The return statement in step number 2.2 is return 3*fact(2) becomes
return 3*2 which is again retruns the number 6 to the function called from "printf("\n The Factorial of %d!=%d",x,fact_n(x)); "
which gives the final result.
The Factorial of 3!=6.
Previous Topic:-->> Nesting of Functions in C || Next topic:-->>Passing array to Function.
Other Topics:
Variables and Identifiers
Relational Operators
if-else statements
Switch case
While Loop
Infinite while Loops
C FOR Loop
Infinite for Loops
Continue in Loops
One Dimensional Array
Two Dimensional Arrays
Read and Display 2D Arrays
Types of functions
Passing Array To Functions
Nesting of Function
Array vs Structure
Array of Structure
Structures and Functions
Structures Within Structures
Use Of Pointers In C
File Handling In C
Loops FAQ
Arrays FAQ
count vowels in a file
Function FAQ
Conditional Statements Assignments
For Loops Assignments
Arrays Assignments
Function Assignments
Structure Assignments
Pointers Assignments
Files Assignments
Storage classes Assignments
Binary Files
count words,lines in a file
Copy files
Update File
Continue in Loops
break in Loops
Difference Between While and Do while
difference while do..while & for
malloc
calloc
Storage Classes
Operators MCQ
Conditional Statements MCQ
Loops MCQ
Arrays MCQ
Function MCQ
Structure MCQ
Pointers MCQ
Files MCQ
Storage classes MCQ