explain how the recursion works...

0 Ankit G · October 10, 2016
when the facto finder is set to be 5 it goes round and around until its equal to 1..

but when it is equal to 1 we have returned 1...where that 1 goes ?why it is not printed..?

:'(help!!!!

Post a Reply

Replies

Oldest  Newest  Rating
0 Ryan Carr · December 22, 2016
Recursion is one of the most difficult subjects in programming to grasp. I don't have the recursion code in front of me, but I'll try to remake it off the top of my head.

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

In order to simplify things we will start with smaller numbers when we call factorial().

factorial(1) // When we run this x == 1 so the function returns one. If you check this against google type in 1! you'll get an answer of 1

factorial(2) // This time x == 2, so the if is false. We drop into the else portion and we are told to return 2 * factorial(2 - 1), 2 - 1 == 1 so we're calling factorial(1)
factorial(1) // From our earlier test we know that this will return the number one. So we can imagine replacing return 2 * factorial(2 - 1) with return 2 * 1 
This gives us the result of 2, again we can test this in google by typing 2! and google calculator returns 2. Each time you increase the starting number you increase the number of times factorial gets called.

factorial(3) // This time I'll nest all the returns so you can see the math   return 3 * factorial(2) * factorial(1) or 3 * 2 * 1 which == 6
factorial(4) // Let's nest them again return 4 * 3 * 2 * 1 == 24


So basically as you step backwards out of the recursion you take whatever value was returned and multiply it by the current x and return that. This is why you never see 1 being returned at the user level.. The only number that is printed is the final number after all the values have been returned and multiplied together.
0 Barbus Bogdan · December 22, 2016
Hi, i saw that no one replied to you so i made an account to help you out. I understand how recursion works, but excuse me if i won't be able to explain it to you well.
Open the recursion tutorial at 6:22 and pause the video, now you got the code right in front of you and i will eplain line to line and what the code does.

Bucky enters the value 5, the code verifies if it is equal to 1, it is not so it moves on to else and returns the value x*factorialFinder(x-1). plot twist: x=5
and it is saying hey i know who is x but who the hell is factorialFinder(x-1), and it does factorialFinder(x-1) which is factorialFinder(4), 

4 it is equal to 1?... no then go to else 4*factorialFinder(3),, okay who is factorialFinder(3)? lets find out, the function auto-calls herself until returns 1

3 equal to 1? no then 3*factorialFinder(2).... auto-calls again

2 equal to 1? no then 2*factorialFinder(1)... auto-calls again

1==1? yes then return 1 meaning that factorialFinder(1)=1;

soooo
2*factorialFinder(1)=2*1=2
soo factorialFinder(2)=2 okay so we know this end we move backwards even more until we reach 5*factorialFinder(4)
3*factorialFinder(2)=3*2=6
factorialFinder(3)=6
4*factorialFinder(3)=4*6=24
factorialFinder(4)=24
5*factorialFinder(4)=5*24=120

in other words when you insert 5 as x you get: 5*4*3*2*1 which is 5! :D i hope i helped you out and did not reached too late, hope you understood
  • 1

C++

130,949 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator