Friday, May 7, 2021
More

    Recursion In C Language

    Must Read

    Programmerhttp://www.improgrammer.net
    We started this site to inspire young minds to motivate and encourage them towards Programming Language. In this site you will get programming tutorials, tech, programming facts, programming fun and programming blogs.

    In the C programming language, a recursive function is a special function that calls to itself in a program and this process is known as Recursion. Like other programming languages, C language allows you to define recursive functions easily in the program.

    When we use recursion function it is important to define an exit condition from the function, otherwise, it will go in an infinite loop. Let’s see an example.

     abc()
    {
      printf("Recursion Example \n");
      abc(); // function call itself 
                   ( abc() function calls the abc() function again)
    }

    This is an example of Recursion. When we execute the above code it calls the abc function and prints the statement “Recursion Example” and call the abc function again. abc function is again called from inside the abc function. So this function will keep on printing Recursion Example until the program run out of memory. A recursive function must have at least one exit condition that must be satisfied with a program. Otherwise, the C recursive function will call itself indefinitely until a stack overflow error. As we have seen in the above code, there is no exit condition, it prints until the program run out of memory.

    A general form of the recursive function is as shown below.

    void recursive_function() {
       
       ... ...
       recursive_function(); /* function calls itself */
       ... ...
    }
    
    int main() {
      
      ... ... 
      recursive_function();
      ... ...
    }

    A recursive function executes the problem by dividing it into the subtasks. The solutions are then combined to produce the solution to the original problem. This is a famous programming technique called divide and conquers in c language. There is a stop condition defined in the function which is satisfied by some specific subtask. When the recursion stops and the final result is returned from the main function.

    Recursion Function Used for

    A recursive function is very useful to solve many mathematical problems like

    • Calculate factorial of a number
    • Generating Fibonacci series
    • Tree Traversals
    • Sorting
    • Searching
    • DFS of Graph
    • Towers of Hanoi, etc.

    Advantages and Disadvantages of Recursion

    • Recursion is more elegant and requires a few variables which make the program clean. Recursion can be used to replace complex nesting code by dividing the problem into the same problem of its sub-type.
    • In the other hand, it is hard to think the logic of a recursive function. It is also difficult to debug the code containing recursion.

    Direct and Indirect Recursion

    When a function calls the same function it is called direct recursive and if any function like a(), call another function like xyz(), and xyz function call a() it is called indirect recursive. Here we have described the direct and indirect function as shown below.

    Direct recursion

    void a()
    {
        // Some code....
    
        a();
    
        // Some code...
    }

     Indirect recursion

    void a()
    {
       xyz();
    }
    void abc()
    {
         a();
    }

    Example: Recursive function for factorial

    #include <stdio.h>
    
    int factorial( int n) {
    
       if(n<= 1) {
          return 1;
       }
       return n * factorial(n - 1);
    }
    
    int  main() {
       int n,f;  
        printf("Enter the number ");  
        scanf("%d",&n); 
       printf("Factorial of %d is %d\n", n, factorial(n));
       return 0;
    }

    Output

    Enter the number 5
    Factorial of 5 is 120

    Fibonacci series using a recursive function

    #include<stdio.h>
    
    int Fibo(int);
    
    int main() {
        int n, i = 0, c;
    
        printf("enter number");
        scanf("%d", &n);
    
        printf("Fibonacci series\n");
    
        for (c = 1; c <= n; c++) {
            printf("%d\n", Fibo(i));
            i++;
        }
    
        return 0;
    }
    
    int Fibo(int n) {
        if (n == 0)
            return 0;
        else if (n == 1)
            return 1;
        else return (Fibo(n - 1) + Fibo(n - 2));
    }

    Output

    enter number5
    Fibonacci series
    0
    1
    1
    2
    3
    

    See More: Best WordPress Hosting Providers

    Latest Articles

    More Recipes Like This