Recursive Functions in C

Recursive Functions in C

In this article, I am going to discuss the Recursive Functions in C with examples. Please read our previous articles, where we discussed the Local Vs Global Variables in C. At the end of this article, you will understand the following pointers.

  1. What is Recursion?
  2. How does recursion work in C?
  3. What are the advantages of recursive functions in C?
  4. What are the disadvantages of recursion in C?
  5. How recursive functions are classified?
  6. Can we call the main function itself in C?
What is Recursion?

Function calling itself is called recursion. The function in which control is present, if it calls itself again then it is called recursion process. Recursion is a process by which function calls itself repeatedly until some specified condition has been satisfied. The process is used for repetitive computation in which each action is stated in terms of a previous result. Many iterative problems can be written in this form.

In order to solve a problem recursively, two conditions must be satisfied. First, the problem must be written in a recursive form, and second, the problem statement must include a stopping condition. If a recursive function contains local variables, a different set of local variables will be created during each call. The variables will represent a different set of values each time the function is executed. Each set of values will be stored on the stack, so that they will be available as the recursive process “unwinds” i.e., as the various function calls are “popped” off the stack and executed.

Syntax:

Recursion in C

Example: To show the use of recursion in C
#include <stdio.h>
void abc()
{
    int a;
    static int s = 3;
    a = ++s;
    printf("\n %d %d ", a, s);
    if(a <= 5)
        abc();
        printf("\n %d %d ", a, s);
}
int main()
{
    abc();
    abc();
    return 0;
}

Output:

Example to show the use of recursion in C

Example: calculate factorial using Recursive Functions in C

int factorial (int n)
{
      if(n==1)
          return (1);
      return(n*factorial(n-1));
}

Here, the factorial function will call itself but with a smaller value of n. The complete program is given below.

#include <stdio.h>
int factorial(int number);
int main() 
{    
    int x = 6;
    printf("The factorial of %d is %d\n", x, factorial(x)); 
    return 0;
}
int factorial(int number) 
{
 	if (number == 1)    
        return (1); /* exiting condition */
  	else
    	return (number * factorial(number - 1));
}

Output: calculate factorial using Recursive Functions in C

We declare our recursive factorial function which takes an integer parameter and returns the factorial of this parameter. This function will call itself and decrease the number until the exiting, or the base condition is reached. When the condition is true, the previously generated values will be multiplied by each other, and the final factorial value is returned. We declare and initialize an integer variable with value”6″ and then print its factorial value by calling our factorial function.

How does recursion work in C?

blank

What are the advantages of recursive functions in C?
  1. Function calling related information will be maintained by recursion.
  2. Stack evaluation will take place by using recursion.
  3. Prefix, postfix, infix notation will be evaluated by using recursion
What are the disadvantages of recursion in C?
  1. It is a very slow process due to stack overlapping.
  2. The recursive program can create stack overflow.
  3. The recursive program can create infinite loops.
How recursive functions are classified?

Recursions are classified into two types

  1. Internal recursive process
  2. External recursive process

If one recursive function is calling itself then it is called the internal recursive process and if one recursive function calling another recursive function then it is called an external recursive process.

Can we call the main function itself in C?

The main() function can be called itself but if we are using auto variable then it becomes stack overflow error. Let us see the program for better understanding.

#include <stdio.h>
int main()
{
    int a = 5;
    ++a;
    printf("%d", a);
    if(a <= 6)
        main();
        printf("%d", a);
    return 0;
}

Output:

Can we call the main function itself in C?

Let us see another program using a static variable

#include <stdio.h>
int main()
{
    static int s = 1;
    ++s;
    printf("%d", s);
    if(s <= 3)
        main();
        printf("%d", s);
    return 0;
}

Output: Recursive Functions in C

Example: to calculate the power of a number using the recursive function in C.
#include <stdio.h>
int power(int b, int e)
{
    if(e < 0)
        return 0;
    else if(e == 0)
        return 1;
    else
        return( b * power(b, e-1));
}
int main()
{
   int a, b, p;
   printf("Enter the value of a : ");
   scanf("%d" , &a);
   printf("Enter the value of b : ");
   scanf("%d" , &b);
   p = power(a, b);
   printf("%d^%d value is %d", a, b, p);
   return 0;
}

Output:

calculate the power of a number using recursive function in C

In the next article, I am going to discuss Adding user-defined functions in C Library with Examples. Here, in this article, I try to explain Recursive Functions in C. I hope you enjoy this Recursive Functions in C article. I would like to have your feedback. Please post your feedback, question, or comments about this article

Leave a Reply

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