Recursion function in C programming 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.