Recursion in c


A function calling itself in the body of the same function is called as recursion.
In c programming, a function can call itself, but there should be a condition to limit the calling of the function, otherwise the function will go into infinite calling. 

Just go through the below program, which is to find whether the given number is Armstrong or not.
You must have seen this code written using looping statements.
But, the code can also be written using recursion.
Have a look at the function definition of the function "armstrong()".
Have a look in the line statement,
s=((n%10)*(n%10)*(n%10)+armstrong(n/10));
here you will see the function armstrong(n/10) is called in the body of the same function. An integer value of (n/10) is passed through the function.
This is called recursion.
If-else statement is used to terminate the calling of the function. When n/10 becomes 0, the else-statement will not be executed.

Example:

#include <stdio.h>
int armstrong(int);
int main() 
{
    int num,temp=0;
    
    printf("Enter the number:");
    scanf("%d",&num);
    temp=armstrong(num);
 
    if(num==temp)
    printf("yes, number is an Armstrong number");
    else
    printf("No, number is not an Armstrong number");
   
    return 0;
}
int armstrong(int n)
{
    int s=0;
    if(n==0)
    return 0;
    else
    {
    s=((n%10)*(n%10)*(n%10)+armstrong(n/10));
    }
    return s;
}

How does recursion works?

Algorithm:
1. In the main function input "num" is taken.
2. A function declared "int armstrong(int)" is called and, the "num" value is passed through the function as an argument.
3. In the function definition, the value gets initialized to the parameter ( int n).
4. consider, n=153
   The value enters into the body of function definition.
   The condition is checked if(n==0), the condition becomes false, curser goes to else part.
   In else part, An algorithm to calculate the armstrong is followed. The result will be stored  in variable s.
  The alogrithm is, s=((n%10)*(n%10)*(n%10)+armstrong(n/10)).
  Let's see the flow of the algorithm.
  Initially, n is 153.
  n%10=153%10=3.
  n/10=153/10=15.
  s=((3*3*3)+armstrong(15)).
    "armstrong" is the function that is called by the same function. This is called a recursive call.
    The cycle will repeat from step 4. Now, the value of n is 15.
    The cycle will continue until n is 0.
 Observe the below figure. You will get an idea of how recursion works.

Recursion-c

 

Stack plays an important role in recursion.

Use of stack in recursion.

Stack follows the rule of last in first out. We all know this about Stack, Then how is stack used in recursion?
In any program, whenever the variable is declared, it is stored in the stack. The variable stays in the stack until the program is terminated. If the variable is declared in the main function or, any other function, the life span of the variable is until the function gets terminated.
In recursion, whenever the function calls itself, the value of the previous call is stored in the stack. At every call, the value gets pushed into the stack. When the function returns the value, the value is executed and popped in the reverse order.

Push operation:

stack-operation

stack-operation

stack-operation

stack-operation

 Pop operation:

 

stack-operation


stack-operation


Advantages and Disadvantage of recursion in c