Close
Close

Can anyone explain recursion in c ?

   Eshan singh , 9th Grader in Delhi Public School,Karnal

I am not able to understand in how a control paasses from one part to another

for example in factorial program using recursion

This is the code

#include <stdio.h>
#include <stdlib.h>
int rec(int);
int main()
{
    int a,fact;

    printf("Enter a Number");
    scanf("%d",&a);


    fact=rec(a);
    printf("Factorial of %d is %d",a,fact);
    return 0;
}
int rec(int x)
{
    int f;
    if(x==1)
        return(1);
    else
        f=x*rec(x-1);

        return(f);
}

Here in function,int rec(int x),If a  value (Let us suppose 5) passes to x , it is checked whether it is etedqual to one or not as it is not equal to one,else block is executed where f=5*rec(5-1=4) so My question is that when one is subtracted from 5 the value 4 passes to x again and whole program get executed and again 4-1 =3 happens and three is again passed to x so how will multiplication takes place  as the number is again again passed to function and stored in x?

 

 

I am stuck in recursion since two weeks…. Please answer as I Want to move forward in C programming.

 


Answers

  •   

    Let’s go step by step.

    As you said, f is f=5*rec(5-1=4). So, now rec(4) will be called and it will return 4*rec(4-1) i.e., 4*rec(3). Thus, the initial expression will become – f = 5*4*rec(3) (as 4*rec(3) is returned from rec(4)). Again, the whole program will run for rec(3) and will return 3*rec(2). Then the expression will become – f = 5*4*3*rec(2). Similarly, rec(2) will be called and will result into 2*rec(1) and making the expression – f = 5*4*3*2*rec(1).

    When rec(1) will call the function, the condition of if will get satisfied and simply 1 will be returned and thus making the whole expression – f = 5*4*3*2*1 (1 is returned from rec(1)).


    • Sir why one will not return to main() function?
      - Eshan singh
    • Sir why one will not return to main() function?
      - Eshan singh
    • 1 will be returned if x is 1 because it is under the if having condition if(x==1)
      - Amit Kumar
    • I last step when rec(1) will be called the condition of if will get satisfied so (1) will not return to main() ?
      - Eshan singh
    • The 1 will return to main and factorial =1. Will be printed and the program will become incorrect ?????????
      - Eshan singh
    • Any value will be from the space from where the function has been called. The rec(1) is called from rec function. So, it will not return the value to main. The main has called rec(5), so the value of rec(5) will be returned to main.
      - Amit Kumar
    • Plz explain the last comment in more detail
      - Eshan singh
    • First time when we enter 1 , the value of a gets photocopied in x then if conditions gets satisfied and 1 is returned to main but why this not in the case when after recursion process ,x becomes 1 ie rec(1) the value of comes out to be 1 but it is not returned to main ?
      - Eshan singh
    • The value will be returned from where the function has been called. rec(1) is called from rec function itself. So, just put the value returned by rec(1) there and that's it. So, 5*4*3*2*rec(1) will become 5*4*3*2*1 and this is the value to be returned by rec(5) which is called from main so, this will be returned from the main. Please read the answer once more. You will get it.
      - Amit Kumar
    • Thanks , got it!
      - Eshan singh

  •    emmausa

    I really like the information you share. I learned a lot of new and useful knowledge from your post. Suika game



  •    jeffreestar

    Recursion in programming is a technique where a function calls itself to solve smaller instances of the same problem until a base case is reached. In your factorial slice master example, the base case is when the input x is equal to 1, in which case the factorial is 1.



Ask Yours
Post Yours
Write your answer