Getting Started With C

Example:-

void fun(int n) { fun(n-1); printf(“%d”,n); } Here we are calling the function.we haven’t given any base condition so the above program will be go to infinite loop.
  • Examples :

    Program.c


  • Output:

                Factorial of 5 is 120
                    

    Explanation

    1. Main Function Call (`factorial(5)`) - Call `factorial(5)` - `n = 5`, not a base case, so it calls `factorial(4)`. 2. First Recursive Call (`factorial(4)`) - Call `factorial(4)` - `n = 4`, not a base case, so it calls `factorial(3)`. 3. Second Recursive Call (`factorial(3)`) - Call `factorial(3)` - `n = 3`, not a base case, so it calls `factorial(2)`. 4. Third Recursive Call (`factorial(2)`) - Call `factorial(2)` - `n = 2`, not a base case, so it calls `factorial(1)`. 5. Fourth Recursive Call (`factorial(1)`) - Call `factorial(1)` - `n = 1`, a base case, returns 1. 6. Back to Third Call (`factorial(2)` returns 2 * 1) - `factorial(1)` result (1) is multiplied by 2, returns 2. 7. Back to Second Call (`factorial(3)` returns 3 * 2) - `factorial(2)` result (2) is multiplied by 3, returns 6. 8. Back to First Call (`factorial(4)` returns 4 * 6) - `factorial(3)` result (6) is multiplied by 4, returns 24. 9. Back to Main Call (factorial(5) returns 5 * 24) - `factorial(4)`result (24) is multiplied by 5, returns 120. 10. Result Displayed in main Function - Print: "Factorial of 5 is 120."

    In detail Explanation of recursion

  • Examples :

    Program.c


  • Output:

              1 2 3 4 5
            

    Explanation

              Function calls:
            First from main fun(5) calls with 5  value .
            Iteration1:-
            Checking if statement if n==0 if not execute the remaining statement
            1. fun(5-1)-->fun(4)
              Below we left with printf(“%d”,n); that  statement is push to the stack see the below diagram
            Iteration2:-
            Checking if statement if n==0 if not execute the remaining statement
            2. fun(4-1)-->fun(3)
              Same as above the printf statement is push to the stack
            Iteration3:-
            Checking if statement if n==0 if not execute the remaining statement
            3.fun(3-1)--> fun(2)
              Same as above the printf statement is push to the stack
            Iteration4:-
            Checking if statement if n==0 if not execute the remaining statement
            4.fun(2-1)-->fun(1)
              Same as above the printf statement is push to the stack
            Iteration5:-
            Checking if statement if n==0 if not execute the remaining statement
            5.fun(1-1)-->fun(0)
            Iteration6:-
              if(n==0)
             condition is true so the function will return 0
             then the remaining statements which are in statck will be executed sequentially .
            
            

    Resources :

    Test Your Knowledge
    Choose The

    With supporting text below as a natural lead-in to additional content.

    Q1.

    Consider the following recursive function in C:
                              int fun(int n) {
                                  if (n <= 1)
                                      return 1;
                                  else
                                      return n * fun(n - 1);
                              }
                              What does this function compute?
                          

    Q2. What is a potential issue with recursive functions in C?