i perfectly understand how it works. as "a concept" it's easy to grasp... but in practice, i'm not sure that i can come up with my own example to code.
yea, I went over recursion last term. I didn't watch this video when I needed it, but it does a better job of explaining how and why recursion works the way it does than the video my CS162 teacher provided. However that's probably because teachers are forced to teach a certain way unless, they're tenured.
Bucky all I gotta say is you're a str8 up boss for explaining c++ with such good analogies and clarity. If you were my teacher I'd probably switch my major from EE to CE. You're my main study guide for finals, thanks for all the help man really appreciated.
Hey bucky..doing a great job pal! I'm from India and do appreciate your efforts for making the concepts easy and by keeping it simple and clear, you are helping a lot!
@@HermanWillems art degrees don't contribute to the betterment of humanity and are far from being a part of stem, just saying. No hate to anyone doing what they love
@@DracoMalfoy-yh3ii You have no right to say that. You have 0 knowledge of the impact of art on the people of this world. Many designs and ideas are visualised through art. You never know, maybe the greatest inventions of this world were thought of whilst listening to Beethovens Fifth Symphony. Im not an artist, I am an engineering student. You are very ignorant and very wrong.
+uniikkiz Lol for some reason I'm getting 5 4 3 2 120 Like, I'm pretty sure that's not what he was talking about. I think I know what he was saying, I just want to know what the program is doing exactly.
Dude, your super duper good when it comes to programming. I just love watching and learning from you. Trust me, I'm doing good at the moment when it comes to programming. Thanks to you. Thanks once again.
short and crisp!! the concept of recursion is loud and clear!!!! your method of teaching with fun is good and it has made the session interesting!!!!! keep up the good work,sir!!
I don't get it, returning 1 means that it should print out 1, right? I understood the whole thing but I can't simply understand why returning 1 would return 120. Someone please reply!
+Samin Ishmam Well, the way I understand it is like this, so if we follow the number 5 through it goes something like this, we at first return 5*(whatever the function returns) which is like 5*4*(whatever the function returns), then 5*4*3*(whatever the function returns) and it keeps going till it hits 1 and then what it returns is 5*4*3*2*1, so it kind of goes like this. Pretty weird, doesn't exactly works like other aspects of programming because the storage of the number isn't really like anything else in c++. I understand why he said it's a widely hated thing, when I learned it in school isn't wasn't too widely hated but we moved through it pretty quickly with a fibonacci sequence, I can imagine how annoying it would be to program some giant using a bunch of recursion to solve problems.
The first call, it returns 5 * the output of the second call, which is 4 * the output of the 3rd call which is 3 * the output of the 4th call which is 2 * the output of the 5th call which is 1. Then all the nested function calls “unwind” off the stack, leaving you with 1 * 2 * 3 * 4 * 5.
well after changing the return values i found out the return 1 means 1 times the older values, if u did return 10 it would multiply it by 10 leaving 1200
Actually a few months later and i like recursion. Bucky makes it seem more complicated than it should be. (Which is surprising because usually he makes things simple.)
That was the most fun I've had doing a Bucky tutorial! I set mine up to say "5*4*3*2*1 aka 5! = 120". And had the same format work for whatever number I put! Microsoft here I come.... after several years of more learning.
This was quite easy to understand, probably thanks to the way in which he explained it. From my understanding recursive function is just the ability for a function to call itself. A base point is just a statement to end the continuous cycle of loops. When using to find a number factorial it's just like calling the function again and again until it reaches a base point, than it multiplies it all and returns the final value. At least that's my understanding.
4:04 just a note, when you have a "return" in an if statement you don't need to include the else just write your line of code directly. Because if the "if" statement executes the function will return the value and stop running (it won't look at the rest of the code).
My College Proff. took 3 lectures to try and explain this to us/me. each lecture an hour and 15 minutes,Didn't understand crap. you took 8:19 minutes to make me a master of recursion xD haha, thanks alot!~
isn't it slower than simply calculating facorials using loops? i mean, this function has to run until it returns 1 eventually, and than it goes back to calculating. looks like double the work. or am i missing something?
As soon as it reaches 1, it passes the 1 up 1 level at a time. So using 5 as an example: initially it looks like this... 5 (we'll call this A) 5-1 = 4 (B) 5-1-1 = 3 (C) 5-1-1-1 = 2 (D) 5-1-1-1-1 = 1 (E) STOPPING... A(B(C(D(E)))) = 1+1+1+1+1(1+1+1+1(1+1+1(1+1(1)))) = 120 Basically the 5, 4, 3, and 2 are forgotten, and are replaced by the number of recursions the function made before the end case is reached. So 1 is the end case of 1, and 2 is the end case + 1. 5 is the end case + 4.
lol,recursion is really something which i always hates and most of teachers are stupid to explain it well because they themselves don't know that what the heck actually recursion is ,thanks for this awesome video bucky,cheers hun :)
THANKS BUCKY U R DA BEST ! i put a global variable as 0...and then i used it to show how many times the factorialFunction was called...now its a lot easier to understand ..and returning 1 or 0 is same as returning true or false.. just in case if someone does't know using namespace std; #include int total = 0; int factorialFunction(int x){ total++; if(x == 1){ cout
To everyone not understanding why the "return 1" statement is not printing 1 instead of 120: Think of the calculation like so: x * (factorialFinder). X starts at '5', then you want to find the number to multiple it by, so you call yourself again 'factorialFinder' to get the number '4', this loops all the way down to 1, so it would end up like x (2) * factorialFinder (1) The x==1 and return is to stop it looping around again, if you took the x==1 return statement out it would still return 1, but then it would keep going into minus figures. Hope that clears things up for people.
I wanted to calculate the factorial of 20 using this but it gave out a negative number. I checked again then I noticed that it gives perfectly fine till 16 but starts giving negatives from 17. Why so? Thanks in advance.
hey guys, when the function finally reaches zero and returns 1, then runs main, and calls itself again. I do not understand why it does not repeat itself again, but prints 120. How does it print 120. Someone please help me
EDIT: Soz, I didn't notice the date. It returns 5 different times, multiplying the most recent x return value with the new one. FF(5) =/= 1, so run FF(5-1) FF(4) =/= 1, so run FF(4-1) FF(3) =/= 1, so run FF(3-1) FF(2) =/= 1, so run FF(2-1) FF(1) = 1, return. Now, on the returns, you multiply by the x value, then return that value for the preceding functions to finish: 1*2 = 2 2*3 = 6 6*4 = 24 24*5 = 120
I made a program that calculated the results of all possible ways how a tic tac toe game could end (it's about 355'000 of 'em). Made it by nesting 9 for loops into each other. Now recursion is a new idea. But I think in my case, my program built with recursion would be easier to understand, but slower at runtime. Interesting stuff :D
Well then ... there is the beauty :D Every for loop stands for one move. Every time it iterates, checkForEnd() checks whether the move would end the game and can therefore skip the execution of all lower order for loops. writeResult() prints the outcome into a txt file, either in a checkForEnd() or if the lowest order for loop didn't make a winner. Beauty of math, I'd say :) There's the for loop part. The tabs were removed at pasting, but it actually looks somehow like that: for A for B for C for D d c b a Calculating 15000 possibilities took 12 minutes on my 2.3GHz Processor xD Result was 450KB of these lines: Ooo || 1-2-3-4-5-------- TIE || 1-2-3-4-5-6-7-8-9 xxX || 1-2-3-4-5-6-7---- Enjoy! --- // 1-a for ( int a = 1; a < 10; a++ ){ fC[a] = 'X'; mF[1] = a; fF[a] = false; // 2-b for ( int b = 1; b < 10; b++ ){ if ( fF[b] ){ fC[b] = 'O'; mF[2] = b; fF[b] = false; // 3-c for ( int c = 1; c < 10; c++ ){ if ( fF[c] ){ fC[c] = 'X'; mF[3] = c; fF[c] = false; // 4-d for ( int d = 1; d < 10; d++ ){ if ( fF[d] ){ fC[d] = 'O'; mF[4] = d; fF[d] = false; // 5-e --- Win possible from this move on for ( int e = 1; e < 10; e++ ){ if ( fF[e] ){ fC[e] = 'X'; mF[5] = e; if ( !checkForEnd(5) ){ fF[e] = false; // 6-f for ( int f = 1; f < 10; f++ ){ if ( fF[f] ){ fC[f] = 'O'; mF[6] = f; if ( !checkForEnd(6) ){ fF[f] = false; // 7-g for ( int g = 1; g < 10; g++ ){ if ( fF[g] ){ fC[g] = 'X'; mF[7] = g; if ( !checkForEnd(7) ){ fF[g] = false; // 8-h for ( int h = 1; h < 10; h++ ){ if ( fF[h] ){ fC[h] = 'O'; mF[8] = h; if ( !checkForEnd(8) ){ fF[h] = false; // 9-i for ( int i = 1; i < 10; i++ ){ if ( fF[i] ){ fC[i] = 'X'; mF[9] = i; if ( !checkForEnd(9) ){ writeResult(9, RES_TIE); } i = 10; mF[9] = 0; fC[i] = 'E'; } } // end 9-i fF[h] = true; } mF[8] = 0; fC[h] = 'E'; } } // end 8-h fF[g] = true; } mF[7] = 0; fC[g] = 'E'; } } // end 7-g fF[f] = true; } mF[6] = 0; fC[f] = 'E'; } } // end 6-f fF[e] = true; } mF[5] = 0; fC[e] = 'E'; } } // end 5-e fF[d] = true; mF[4] = 0; fC[d] = 'E'; } } // end 4-d fF[c] = true; mF[3] = 0; fC[c] = 'E'; } } // end 3-c fF[b] = true; mF[2] = 0; fC[b] = 'E'; } } // end 2-b fF[a] = true; mF[1] = 0; fC[a] = 'E'; } // end 1-a
WHO WILL WIN? $100,000 college tuition, tenured professors, $200 textbooks....or a 7 minute video from Bucky? He is a gift to Mankind. They should give Nobel Peace Prizes to people like him.
It's nothing to hate, seems pretty logical, you tie so many unknowns together, and then, you just equal one of those equations to a value and the rest gets solved in a chain reaction
He kinda explained it a bit wrong mathmetically.. It's not 4 factorial and 3 factorial and 2 factorial.. The codes starts with 5.. the function runs and call itself and runs again but this time with 4.. etc.. so what it's going on here is just: 5 * (5-1) * (4-1) * (3-1) * (2-1)
Yup, there are many other common uses for recursive function, for instance we can make a function that shows nth term of fibonacci series int fib(int n) {if (n==1 || n==2){return 1;} else (return (fib(n-1)+fib(n-2);}
return "gives" back a value; within the return statement he is also calling a function, so before the return value you can "give" back the value, it needs to execute the other function, and so on and so forth until it finally gets to the return 1; which DOES NOT have another function attached and therefore it can finally begin giving back to earlier functions
My Java teacher used to hate me because I always used recursion in all of my programs, even if I didnt need it. I managed to get recursion to occur on my second day of class and did it every day after. Recursion is fun!
hey Bucky, nice videos, short and simple, I know you don't want to confuse beginners, just to let you know that above 12! (12 factorial) the program will not work properly, you get a negative value instead and even after that around 34 you get a 0. maybe in your later tutorials you deal with how to handle large numbers or calculations. I have some experience of programming but just readjusting to C++ using visual studio and this is a good revision course for me as well as learning new things, I wrote in octave more recently which is like matlab and similar to C++.
4:47 - 4:52 Doesn't it just call 5 x (5-1) i.e. 5 x 4? I mean the factorial function is being defined by the code. I don't think it's a preexisting function like rand(), is it? So then how does it know that (x-1) = 4! ? I think it would just read the code as being 5 x (5-1).
a for loop would be more or less the same amount of lines but recursion comes in handy for more complicated problems where loops become more tedious but recursion requires more thought because its more elegant.
OK, I looked around and it seems as if there is little to no use for recursive functions UNLESS you are dealing with trees. Even then, it inferior in memory usage and speed to loops and other solutions
if you didn't understand, here's how it works: loop 1: x is not equal to one so run else statement which is: x * factorialfinder(x-1) for that we need value of favtorialfinder(x-1) which is found in loop 2. x-1 is now the new value of x for next loop loop 2: x is not equal to one so run else statemement which is: (x-1) * factorialfinder ((x-1) -1) for that we need to find value of factorialfinder((x-1) -1 ) which is found in loop3. ok now that i think about it i think there's not really a way to explain this in a simpler way..i think best way to understand is write the code down on a piece of paper and see how each loop works
I thought the same, but if you use a loop you have to have another variable that stores the values. This also allows for more than one exit point while still using the 'return x;' . This is all I could come up with, but I am only learning this stuff for the first time as well.
I haven't heard about this in class, or anywhere ever, I haven't even watched the video yet, but I only read the name and I already think this is going to be some mind twisting unnecessary stuff. Edit: I know I'm probably going to regret saying this but this seems failry easy, fluid and really usefull.
I know how to do this but i don't understand the logic behind it.Can someone help ? Why do we type return 1 ? Why when the function ends it gives us 5x4x3x2x1 ?Because when we type return x*factorialFinder(x-1); i think it would go like this 5x4 and then 4x3 3x2 2x1
The 'return 1' inside the if statement would be the same as returning x, since x == 1. When we call the function 'factorialFinder(5)', the program goes through this process: 5 != 1, so we move into the 'else' block, where the program returns '5 * factorialFinder(5 - 1)'. The program has to return a singular integer, so it has to evaluate that entire expression before it can go through with that return, which means it immediately dives into 'factorialFinder(5 - 1)', or 'factorialFinder(4)', which leads it to yet another return that it has to evaluate before it can complete. It follows this all the way down to 1, where it finds a return it CAN complete, and goes through with it. Once the program evaluates '2 * factorialFinder(1)' it becomes '2 * 1', which is 2, a single integer, so the program can return that to the next-in-hierarchy statement, '3 * factorialFinder(2)'. Evaluate, return, repeat until it finally reaches the initial statement: '5 * factorialFinder(4)'. At this point, what the program technically returns is '5 * (4 * (3 * (2 * 1)))' it has to evaluate 2 * 1, which it gives to 3 * 2, and so on until it works it's way back up to the top.
Basically, in the first loop you're multiplying 5*(something you don't know), and that something is 4! of course but the program doesn't know, so the second member of multiplication will just call the function again, but when you do it you won't find 4! immediatly, just that 4! is 4*something(3!) - it will need to call the function again and again, until you know that something = 1. Then the program can go backwards and solve all the pendent functions. It can be very confusing the first time
Always love Bucky's tutorials how they make learning programming easy. But in this one, like every other teacher, bucky tried to teach recursion in a very simple way and that simple way was not enough for me to understand recursion clearly. And so, I had to learn recursion from stackoverflow. But still love the way you teach.
I had some major issues with this but writing it out and following exactly what the computer did helped me a bunch. x=5 we run ff(5) we get 5 * ff(4) we run ff(4) we get 4 * ff(3) so we run ff(3) we get 3 * ff(2) so we run ff(2) we get 2 * ff(1) so we run ff(1) we break the recursion here because x=1 so we run the if code we get 1 so essentially ff(5)= 5 * ff(4) ff(4)= 4 * ff(3) ff(3)= 3 * ff(2) ff(2)= 2 *ff(1) we break the recursion here because x=1 so we run the if code ff(1)= 1 now we can solve 2*ff(1)=2 now we solve 3*ff(2)=6 now we solve 4*ff(3)=24 now we solve 5*ff(4)=120
i think other way, we give value of x = 5 so the code run until if x==1--this is false,so else is true,then is 5*(x-1=4)=20 then go over 20*3=60, 60*2=120 now is x==1 so return 1 so answer 120
Yes a loop would be easier, but a recursion is for when you've finished your loop and you want to increase efficiency in your program. In huge programs the accumulative recursions will make your program significantly faster than if they were just loops.
@johnthesman factorialFinder(5){a} {a}return x*factorialFinder(4){b}; {b}return x*factorialFinder(3){c}; {c}return x*factorialFinder(2){d}; {d}return x*factorialFinder(1); {d} returns 1, which puts 1 into {c} where x is 2 and so returns 2, which can now put this into {b} where x is 3 and returns 6, which can now be put into {a} where x is 4 which returns 24, which can now return to the original factorialFinder and finally multiply the original 5 by 24, returning 120. Hope that makes sense.
another thing that i don't really understand is the first line : int factorial (int a) so we declare the integer factorial, but why another int in the brackets of factorial? how do you call what's inside the brackets? so i guess there are 2 int declared, "factorial" and "a"...
I don't get this. Where is factorialFinder getting the final answer from? I can see that its multiplying 5 by 4 becuase of (x-1). BUT how does it know to multiply 20 by 3 and then 60 by 2? How is it storing the answer from before and multiplying it by the new one?
@spidey678 Recursion means, that within a method/function, you call the same method/function (or one that calls the first one) to accomplish a backwards loop. If done right, it'll do anything you could do with a While or For loop, but backwards. If done wrong, it'll do anything you could do with a While(true) loop. But since it calls itself within itself, I made the inception joke.
@ryangr0 Yes, this exact case would be easier using a loop.There are things tho that cant be made with regular loops.Say the user has to enter a number x and a number n and the program has to compute x^n (x*x*x n times).If you tried to do it with a loop like for (int i=1; i
Even if you give a recursive function an ending point, is it still possible you could give it such a big ending point it would crash before it got there?
You managed to explain something that could have been really difficult in a real simple way, all in 8 minutes. You are a true gift.
He didnt go in depth thats y
y so salty
true, but his explanation set the ground really well to allow anyone who is interested in examining the concept further in depth to be able to do so
in 4 min, cuz i watch all the tutorials in 2x speed😂😂😂😂
seriously so true
i perfectly understand how it works.
as "a concept" it's easy to grasp...
but in practice, i'm not sure that i can come up with my own example to code.
@Jeremiah Givens instead of using if and else if just use condition if (n==0||n==1)return n;
Literally me right now I understand the concepts just don't know how to go about the code in my assignment.
th-cam.com/video/hyRzHZxJIS0/w-d-xo.html
you could try making an exponent power recursive function which takes in base and the power gives you the result
Who knew a video from 8 years ago, when I was in middle school, would help me in college.
yea, I went over recursion last term. I didn't watch this video when I needed it, but it does a better job of explaining how and why recursion works the way it does than the video my CS162 teacher provided. However that's probably because teachers are forced to teach a certain way unless, they're tenured.
same🤣
11 years duh!
Bucky all I gotta say is you're a str8 up boss for explaining c++ with such good analogies and clarity. If you were my teacher I'd probably switch my major from EE to CE. You're my main study guide for finals, thanks for all the help man really appreciated.
I am so sad I only stumbled upon this towards the end of the semester. Thank you so much, you're a lifesaver!
Did you graduate I hope you did Im just curious lol
@@hussianhani4428 haha yes
@@natalien8560 Thats cool!! How did you find the job? also did his tutorials really help?
Better way to understand recursion is just think about Nolans' "Inception"
Hats off to your humorous way of teaching stuff :)
Hey bucky..doing a great job pal! I'm from India and do appreciate your efforts for making the concepts easy and by keeping it simple and clear, you are helping a lot!
+udit koolwal yeah u r right even a person like me can understand this pretty easily
Why are there so many Indian people programming. Why not get an art degree or something? Just asking.
@@HermanWillems art degrees don't contribute to the betterment of humanity and are far from being a part of stem, just saying. No hate to anyone doing what they love
@@DracoMalfoy-yh3ii You have no right to say that. You have 0 knowledge of the impact of art on the people of this world. Many designs and ideas are visualised through art. You never know, maybe the greatest inventions of this world were thought of whilst listening to Beethovens Fifth Symphony. Im not an artist, I am an engineering student. You are very ignorant and very wrong.
Adding some couts helped me to understand whats going on:
int recursion(int x) {
if (x == 1) {
cout
+uniikkiz Lol for some reason I'm getting
5
4
3
2
120
Like, I'm pretty sure that's not what he was talking about. I think I know what he was saying, I just want to know what the program is doing exactly.
+Seoul Peterson did u forget the "return 1; " ?
Probably, I don't have the code in front of me right now.
Nope, got the return 1;
I was missing cout
if(x==1){
cout
Dude, your super duper good when it comes to programming. I just love watching and learning from you. Trust me, I'm doing good at the moment when it comes to programming. Thanks to you. Thanks once again.
short and crisp!! the concept of recursion is loud and clear!!!! your method of teaching with fun is good and it has made the session interesting!!!!! keep up the good work,sir!!
Already into this tutorial playlist since yesterday, much more useful and better explained than 9 weeks of a crappy lecturer at University. :)
He explained it so well
Bucky is the man
He is doing really challenging things so effortlessly
I just started programming classes and I have a feeling that I am going to be watching your videos a lot more in the future. Thanks.
Best one I've seen! I was watching your video and I was like "Hey, I'm finally getting it!" And then I saw "Oh, It's TheNewBoston"! Thanks
this is still relevant 9 years later, ty bucky!
I don't get it, returning 1 means that it should print out 1, right? I understood the whole thing but I can't simply understand why returning 1 would return 120. Someone please reply!
+Samin Ishmam Well, the way I understand it is like this, so if we follow the number 5 through it goes something like this, we at first return 5*(whatever the function returns) which is like 5*4*(whatever the function returns), then 5*4*3*(whatever the function returns) and it keeps going till it hits 1 and then what it returns is 5*4*3*2*1, so it kind of goes like this. Pretty weird, doesn't exactly works like other aspects of programming because the storage of the number isn't really like anything else in c++. I understand why he said it's a widely hated thing, when I learned it in school isn't wasn't too widely hated but we moved through it pretty quickly with a fibonacci sequence, I can imagine how annoying it would be to program some giant using a bunch of recursion to solve problems.
The first call, it returns 5 * the output of the second call, which is 4 * the output of the 3rd call which is 3 * the output of the 4th call which is 2 * the output of the 5th call which is 1. Then all the nested function calls “unwind” off the stack, leaving you with 1 * 2 * 3 * 4 * 5.
well after changing the return values i found out the return 1 means 1 times the older values, if u did return 10 it would multiply it by 10 leaving 1200
it means it starts from x= 5 to toward the base case 1 then it stops there
th-cam.com/video/_OmRGjbyzno/w-d-xo.html
that video helped me, it explained how the computer does the computation
totally worth watching this 8 minute video instead of reading a whole chapter and listening to an hour long lecture
Bucky ... i love your tuts. The names you choose are golden. lol they crack me up.
I am so thankful for this tutorial cuz' I didn't get it when my professor discussed it but you...you just made me understand it in just 8 min. Amazing
Thanks for the tutorials today, Bucky! I made it from 1-30. Ill be back tomorrow to wrap it up!
I don't like recursion.
Actually a few months later and i like recursion. Bucky makes it seem more complicated than it should be. (Which is surprising because usually he makes things simple.)
@@evanmastermind Im still in the "fuck recursion i fucking hate my life" phase lol. hopefully i'll get to your second comment one day
@@DanT-iu6oc what do you think now?
@@stormy1514 wow what a throwback. yeah, i understand it now. takes a while to sink in.
I don’t understand yet:(
Bucky you explained recursion so well I think I already love it. Thank's buddy you are awesome
That was the most fun I've had doing a Bucky tutorial! I set mine up to say "5*4*3*2*1 aka 5! = 120". And had the same format work for whatever number I put! Microsoft here I come.... after several years of more learning.
You just helped me understand this.
This was quite easy to understand, probably thanks to the way in which he explained it. From my understanding recursive function is just the ability for a function to call itself. A base point is just a statement to end the continuous cycle of loops. When using to find a number factorial it's just like calling the function again and again until it reaches a base point, than it multiplies it all and returns the final value.
At least that's my understanding.
recurssion is a hell of a resource, you can find sorting algorithms, tree's paths and all sort of stuff that uses recursion.
4:04 just a note, when you have a "return" in an if statement you don't need to include the else just write your line of code directly. Because if the "if" statement executes the function will return the value and stop running (it won't look at the rest of the code).
WOW!!! This tutorial will make your life easier than listening to your instructor. Nice one Bucky!!! I'll give you some tuna for that!
Thanks you just saved me 2 hours of lecture.
My College Proff. took 3 lectures to try and explain this to us/me. each lecture an hour and 15 minutes,Didn't understand crap. you took 8:19 minutes to make me a master of recursion xD haha, thanks alot!~
isn't it slower than simply calculating facorials using loops? i mean, this function has to run until it returns 1 eventually, and than it goes back to calculating. looks like double the work. or am i missing something?
this is just an example of how they are used. they arent just used to create factorials.
As soon as it reaches 1, it passes the 1 up 1 level at a time. So using 5 as an example: initially it looks like this...
5 (we'll call this A)
5-1 = 4 (B)
5-1-1 = 3 (C)
5-1-1-1 = 2 (D)
5-1-1-1-1 = 1 (E)
STOPPING...
A(B(C(D(E)))) = 1+1+1+1+1(1+1+1+1(1+1+1(1+1(1))))
= 120
Basically the 5, 4, 3, and 2 are forgotten, and are replaced by the number of recursions the function made before the end case is reached. So 1 is the end case of 1, and 2 is the end case + 1. 5 is the end case + 4.
lol,recursion is really something which i always hates and most of teachers are stupid to explain it well because they themselves don't know that what the heck actually recursion is ,thanks for this awesome video bucky,cheers hun :)
THANKS BUCKY U R DA BEST !
i put a global variable as 0...and then i used it to show how many times the factorialFunction was called...now its a lot easier to understand
..and returning 1 or 0 is same as returning true or false..
just in case if someone does't know
using namespace std;
#include
int total = 0;
int factorialFunction(int x){
total++;
if(x == 1){
cout
@Sir Bucky : I thank God evry night b4 sleepin dat he sent a guy like you on earth whose tutorials I watch the whole day like routine work.
To everyone not understanding why the "return 1" statement is not printing 1 instead of 120:
Think of the calculation like so:
x * (factorialFinder). X starts at '5', then you want to find the number to multiple it by, so you call yourself again 'factorialFinder' to get the number '4', this loops all the way down to 1, so it would end up like x (2) * factorialFinder (1)
The x==1 and return is to stop it looping around again, if you took the x==1 return statement out it would still return 1, but then it would keep going into minus figures.
Hope that clears things up for people.
I wanted to calculate the factorial of 20 using this but it gave out a negative number. I checked again then I noticed that it gives perfectly fine till 16 but starts giving negatives from 17. Why so? Thanks in advance.
hey guys,
when the function finally reaches zero and returns 1, then runs main, and calls itself again. I do not understand why it does not repeat itself again, but prints 120. How does it print 120.
Someone please help me
EDIT: Soz, I didn't notice the date.
It returns 5 different times, multiplying the most recent x return value with the new one.
FF(5) =/= 1, so run FF(5-1)
FF(4) =/= 1, so run FF(4-1)
FF(3) =/= 1, so run FF(3-1)
FF(2) =/= 1, so run FF(2-1)
FF(1) = 1, return.
Now, on the returns, you multiply by the x value, then return that value for the preceding functions to finish:
1*2 = 2
2*3 = 6
6*4 = 24
24*5 = 120
Wish I found your channel earlier would've helped me so much this semester. Should I keep trying even if I have to retake the class?
even if you fail the class this time, the stuff you pick up will make it that much easier next time around. Go for it.
@RblxGaming How is the result?
You say recursion is hated, but I think its great. It's one of my favorite things about programming.
Hello World ! Bucky is awesome, believe me . I learnt C++ easily with Bucky's tutorials, thanks a lot Bucky.
I made a program that calculated the results of all possible ways how a tic tac toe game could end (it's about 355'000 of 'em). Made it by nesting 9 for loops into each other. Now recursion is a new idea. But I think in my case, my program built with recursion would be easier to understand, but slower at runtime. Interesting stuff :D
Well then ... there is the beauty :D
Every for loop stands for one move. Every time it iterates, checkForEnd() checks whether the move would end the game and can therefore skip the execution of all lower order for loops.
writeResult() prints the outcome into a txt file, either in a checkForEnd() or if the lowest order for loop didn't make a winner.
Beauty of math, I'd say :)
There's the for loop part. The tabs were removed at pasting, but it actually looks somehow like that:
for A
for B
for C
for D
d
c
b
a
Calculating 15000 possibilities took 12 minutes on my 2.3GHz Processor xD
Result was 450KB of these lines:
Ooo || 1-2-3-4-5--------
TIE || 1-2-3-4-5-6-7-8-9
xxX || 1-2-3-4-5-6-7----
Enjoy! ---
// 1-a
for ( int a = 1; a < 10; a++ ){
fC[a] = 'X';
mF[1] = a;
fF[a] = false;
// 2-b
for ( int b = 1; b < 10; b++ ){
if ( fF[b] ){
fC[b] = 'O';
mF[2] = b;
fF[b] = false;
// 3-c
for ( int c = 1; c < 10; c++ ){
if ( fF[c] ){
fC[c] = 'X';
mF[3] = c;
fF[c] = false;
// 4-d
for ( int d = 1; d < 10; d++ ){
if ( fF[d] ){
fC[d] = 'O';
mF[4] = d;
fF[d] = false;
// 5-e --- Win possible from this move on
for ( int e = 1; e < 10; e++ ){
if ( fF[e] ){
fC[e] = 'X';
mF[5] = e;
if ( !checkForEnd(5) ){
fF[e] = false;
// 6-f
for ( int f = 1; f < 10; f++ ){
if ( fF[f] ){
fC[f] = 'O';
mF[6] = f;
if ( !checkForEnd(6) ){
fF[f] = false;
// 7-g
for ( int g = 1; g < 10; g++ ){
if ( fF[g] ){
fC[g] = 'X';
mF[7] = g;
if ( !checkForEnd(7) ){
fF[g] = false;
// 8-h
for ( int h = 1; h < 10; h++ ){
if ( fF[h] ){
fC[h] = 'O';
mF[8] = h;
if ( !checkForEnd(8) ){
fF[h] = false;
// 9-i
for ( int i = 1; i < 10; i++ ){
if ( fF[i] ){
fC[i] = 'X';
mF[9] = i;
if ( !checkForEnd(9) ){
writeResult(9, RES_TIE);
}
i = 10;
mF[9] = 0;
fC[i] = 'E';
}
}
// end 9-i
fF[h] = true;
}
mF[8] = 0;
fC[h] = 'E';
}
}
// end 8-h
fF[g] = true;
}
mF[7] = 0;
fC[g] = 'E';
}
}
// end 7-g
fF[f] = true;
}
mF[6] = 0;
fC[f] = 'E';
}
}
// end 6-f
fF[e] = true;
}
mF[5] = 0;
fC[e] = 'E';
}
} // end 5-e
fF[d] = true;
mF[4] = 0;
fC[d] = 'E';
}
} // end 4-d
fF[c] = true;
mF[3] = 0;
fC[c] = 'E';
}
} // end 3-c
fF[b] = true;
mF[2] = 0;
fC[b] = 'E';
}
} // end 2-b
fF[a] = true;
mF[1] = 0;
fC[a] = 'E';
} // end 1-a
Dude this is 8 years old, and this fucking helped me so much.... thank you !!!
WHO WILL WIN? $100,000 college tuition, tenured professors, $200 textbooks....or a 7 minute video from Bucky? He is a gift to Mankind. They should give Nobel Peace Prizes to people like him.
thank you bucky, 2 times in a row that you help me
It's nothing to hate, seems pretty logical, you tie so many unknowns together, and then, you just equal one of those equations to a value and the rest gets solved in a chain reaction
True Damage Ekko liked this video
Thank you for the videos. Learning alot for my Software development and programming Modul from you.
He kinda explained it a bit wrong mathmetically.. It's not 4 factorial and 3 factorial and 2 factorial.. The codes starts with 5.. the function runs and call itself and runs again but this time with 4.. etc.. so what it's going on here is just: 5 * (5-1) * (4-1) * (3-1) * (2-1)
Yup, there are many other common uses for recursive function, for instance we can make a function that shows nth term of fibonacci series
int fib(int n)
{if (n==1 || n==2){return 1;}
else (return (fib(n-1)+fib(n-2);}
return "gives" back a value; within the return statement he is also calling a function, so before the return value you can "give" back the value, it needs to execute the other function, and so on and so forth until it finally gets to the return 1; which DOES NOT have another function attached and therefore it can finally begin giving back to earlier functions
Bucky you are doing really well , keep it up Dude.
Bucky ,,u r great man,i watched ur android,iphone,java,& c++ tutorial,amzing,stuff,i realy apreciate ur efforts & the knowledge,kep it up..
dude, im so lucky found your video. im was easy to understand. thanks!!
My Java teacher used to hate me because I always used recursion in all of my programs, even if I didnt need it. I managed to get recursion to occur on my second day of class and did it every day after. Recursion is fun!
hey Bucky, nice videos, short and simple, I know you don't want to confuse beginners, just to let you know that above 12! (12 factorial) the program will not work properly, you get a negative value instead and even after that around 34 you get a 0. maybe in your later tutorials you deal with how to handle large numbers or calculations. I have some experience of programming but just readjusting to C++ using visual studio and this is a good revision course for me as well as learning new things, I wrote in octave more recently which is like matlab and similar to C++.
This lesson's nuts. Every time you run any recursion program, the computer displays that phrase infinitely.
Why would any one hate this ? This is awesome!
Your tutorials are great, crystal clear, keep the good work.
4:47 - 4:52
Doesn't it just call 5 x (5-1) i.e. 5 x 4? I mean the factorial function is being defined by the code. I don't think it's a preexisting function like rand(), is it? So then how does it know that (x-1) = 4! ? I think it would just read the code as being 5 x (5-1).
a for loop would be more or less the same amount of lines but recursion comes in handy for more complicated problems where loops become more tedious but recursion requires more thought because its more elegant.
great video.. just what I needed to help study before a test
OK, I looked around and it seems as if there is little to no use for recursive functions UNLESS you are dealing with trees. Even then, it inferior in memory usage and speed to loops and other solutions
if you didn't understand, here's how it works:
loop 1:
x is not equal to one so run else statement which is:
x * factorialfinder(x-1)
for that we need value of favtorialfinder(x-1) which is found in loop 2.
x-1 is now the new value of x for next loop
loop 2:
x is not equal to one so run else statemement which is:
(x-1) * factorialfinder ((x-1) -1)
for that we need to find value of factorialfinder((x-1) -1 ) which is found in loop3.
ok now that i think about it i think there's not really a way to explain this in a simpler way..i think best way to understand is write the code down on a piece of paper and see how each loop works
This is the most easily explained video
I thought the same, but if you use a loop you have to have another variable that stores the values. This also allows for more than one exit point while still using the 'return x;' . This is all I could come up with, but I am only learning this stuff for the first time as well.
You pretty much saved my ass, late night, need to study for a test, found this!
Thank you so much sir. This is exactly what I needed to study for finals!
I haven't heard about this in class, or anywhere ever, I haven't even watched the video yet, but I only read the name and I already think this is going to be some mind twisting unnecessary stuff.
Edit: I know I'm probably going to regret saying this but this seems failry easy, fluid and really usefull.
Thanks for these videos man. lol totally agree with people hating recursion. It is a definitely a confusing concept.
I know how to do this but i don't understand the logic behind it.Can someone help ?
Why do we type return 1 ?
Why when the function ends it gives us 5x4x3x2x1 ?Because when we type return x*factorialFinder(x-1); i think it would go like this 5x4 and then 4x3 3x2 2x1
The 'return 1' inside the if statement would be the same as returning x, since x == 1.
When we call the function 'factorialFinder(5)', the program goes through this process:
5 != 1, so we move into the 'else' block, where the program returns '5 * factorialFinder(5 - 1)'. The program has to return a singular integer, so it has to evaluate that entire expression before it can go through with that return, which means it immediately dives into 'factorialFinder(5 - 1)', or 'factorialFinder(4)', which leads it to yet another return that it has to evaluate before it can complete. It follows this all the way down to 1, where it finds a return it CAN complete, and goes through with it. Once the program evaluates '2 * factorialFinder(1)' it becomes '2 * 1', which is 2, a single integer, so the program can return that to the next-in-hierarchy statement, '3 * factorialFinder(2)'. Evaluate, return, repeat until it finally reaches the initial statement: '5 * factorialFinder(4)'. At this point, what the program technically returns is '5 * (4 * (3 * (2 * 1)))' it has to evaluate 2 * 1, which it gives to 3 * 2, and so on until it works it's way back up to the top.
Great tutorials. Thanks to you my job interview today was successful 😃
Basically, in the first loop you're multiplying 5*(something you don't know), and that something is 4! of course but the program doesn't know, so the second member of multiplication will just call the function again, but when you do it you won't find 4! immediatly, just that 4! is 4*something(3!) - it will need to call the function again and again, until you know that something = 1. Then the program can go backwards and solve all the pendent functions. It can be very confusing the first time
Always love Bucky's tutorials how they make learning programming easy. But in this one, like every other teacher, bucky tried to teach recursion in a very simple way and that simple way was not enough for me to understand recursion clearly. And so, I had to learn recursion from stackoverflow. But still love the way you teach.
I had some major issues with this but writing it out and following exactly what the computer did helped me a bunch.
x=5
we run ff(5)
we get 5 * ff(4)
we run ff(4)
we get 4 * ff(3)
so we run ff(3)
we get 3 * ff(2)
so we run ff(2)
we get 2 * ff(1)
so we run ff(1)
we break the recursion here because x=1 so we run the if code
we get 1
so essentially
ff(5)= 5 * ff(4)
ff(4)= 4 * ff(3)
ff(3)= 3 * ff(2)
ff(2)= 2 *ff(1)
we break the recursion here because x=1 so we run the if code
ff(1)= 1
now we can solve 2*ff(1)=2
now we solve 3*ff(2)=6
now we solve 4*ff(3)=24
now we solve 5*ff(4)=120
i think other way,
we give value of x = 5
so the code run until if x==1--this is false,so else is true,then is 5*(x-1=4)=20
then go over 20*3=60, 60*2=120
now is x==1 so return 1
so answer 120
there is no hard to get thing when you teach Bucky thnk u
the main benefit of functions is that you can call it several times
whereas for the while loop you can only do it once
Thank you! It was very helpful. You just earned a new subscriber
I LOVE YOU!!! THANK YOU MAN I FINALLY UNDERSTOOD IT
Im going to be doing Comp Sci next year and i find recursion very fun.
th-cam.com/video/hyRzHZxJIS0/w-d-xo.html
@TyrianSword In the case of looping backwards through a string, but there are times when that does the same thing as a positive increment case loop.
I'm not even that far in math yet, but thanks for the little math lesson on factorials xD
Oh NVM, ignore my last comment.
Factorial is pretty easy, i've never done it before, but is just simple multiplication :)
I get this now, thanks :D
I understand that this is a very simple explanation, but if you're going to use int as your data type you should make your base case x
why no return 0 in man function.. wouldn't it throw error snce return type int is mentioned.. hey but it never reaches return
As we have Loops, why do we use Recursion? Loops are way more easy to understand than recursions.
Just subscribed because of your easy explanation
Yes a loop would be easier, but a recursion is for when you've finished your loop and you want to increase efficiency in your program.
In huge programs the accumulative recursions will make your program significantly faster than if they were just loops.
Great tutorial, thanks for making it easy to understand.
@johnthesman
factorialFinder(5){a}
{a}return x*factorialFinder(4){b};
{b}return x*factorialFinder(3){c};
{c}return x*factorialFinder(2){d};
{d}return x*factorialFinder(1);
{d} returns 1, which puts 1 into {c} where x is 2 and so returns 2, which can now put this into {b} where x is 3 and returns 6, which can now be put into {a} where x is 4 which returns 24, which can now return to the original factorialFinder and finally multiply the original 5 by 24, returning 120. Hope that makes sense.
another thing that i don't really understand is the first line : int factorial (int a)
so we declare the integer factorial, but why another int in the brackets of factorial? how do you call what's inside the brackets?
so i guess there are 2 int declared, "factorial" and "a"...
in the starting of the program i used void factorialfinder ( intx ) instead of int factorialfinder (intx) and it was showing some error. why ?
I don't get this. Where is factorialFinder getting the final answer from? I can see that its multiplying 5 by 4 becuase of (x-1). BUT how does it know to multiply 20 by 3 and then 60 by 2? How is it storing the answer from before and multiplying it by the new one?
Finaly i understand this .Thanks a lot for your explanation🙏🏻
Every time I hear recursive functions explained, the example given is always factorial. Are there other common uses for recursive functions?
@spidey678 Recursion means, that within a method/function, you call the same method/function (or one that calls the first one) to accomplish a backwards loop. If done right, it'll do anything you could do with a While or For loop, but backwards. If done wrong, it'll do anything you could do with a While(true) loop.
But since it calls itself within itself, I made the inception joke.
Couldn't I just use a for loop? Or are practical recursive functions too complex to use a for loop?
@ryangr0 Yes, this exact case would be easier using a loop.There are things tho that cant be made with regular loops.Say the user has to enter a number x and a number n and the program has to compute x^n (x*x*x n times).If you tried to do it with a loop like
for (int i=1; i
Even if you give a recursive function an ending point, is it still possible you could give it such a big ending point it would crash before it got there?
when i change the 'return' in the else statement to 'cout'(cout