Seriously Guys put this video into weeks 3 shorts as an addon to the recursion one, this explains it so good!
@marketsareopenmao4 жыл бұрын
Perfect course until right here too! I don't get how they show us that merge sort algorithm then don't tell us how it's working. I assume we are supposed to learn how to build a merge sort then but no way you can do that without perfect knowledge of the call stack.
@GrumpyStoic3 жыл бұрын
@@marketsareopenmao @cs50 Yeah I got stumped in Week 3's Merge sort too. Just happened across this video now so hoping it'll help me progress.
@marketsareopenmao3 жыл бұрын
@@GrumpyStoic lol yeah 11 months later I stopped there... I want to get back to it but it took a while to understand merge sort. Then I got too busy to continue.
@gonzaotc3 жыл бұрын
Good advice.
@djm_8523 жыл бұрын
Yes - for me the recursion short didn't make sense (i.e. HOW does the function know!!) until I saw this. The videos should be merged.
@maximbrykov6 жыл бұрын
Finally, I got recursion and stack frames after your explanation. Thank you Doug.
@thewallstreetjournal56755 жыл бұрын
Finally I see what is meant by a return. It makes no sense without the concept of the stack frame.
@marnierogers39312 жыл бұрын
I took a CS50 course online and have now decided to do a computer science degree thanks to that course. I always come back to Doug's videos when I need a10/10 explaination. Cheers Doug!!
@lsdengo1589Ай бұрын
I have decided to do a computer science degree as well! How is it for you so far?
@ricklangston88956 жыл бұрын
Finally! I was trying to figure out this exact recursion problem without knowing what a call stack was. Then I saw 'call stack' briefly mentioned on Stack Overflow, did a Google search, found this video, watched it, and saw the light at the end of the tunnel. For a while, I thought I was just not able to grasp recursion and that it was a concept enjoyed by only the most sophisticated intellects...kind of like looking at one of those old 3-dimensional posters that contained a hidden image obscured by garble that could only be revealed if you stared at the poster with the right amount of eye blur, mind shift, or focal point. I never did find the 'knack' for looking at those posters and, before watching this video, I was certain I'd have to dismiss recursion as another one of those things I "just didn't get". This video really helped me out. Recursion might not be so strange after all. Thanks.
@JackyVSO4 жыл бұрын
This was great. I was confused after the Week 3 lecture and still confused after the Recursion video but now I get this principle.
@shreephadke36794 жыл бұрын
This video was incredibly easy to understand and the concept was very eloquently taught. Thank you, Doug!
@oussamatarhi64544 жыл бұрын
Frankly, those Shorts are a knowledge treasure. Thank you David J. Malan, thank you Doug Lloyd, thank Harvard.
@albinsopaj4 жыл бұрын
Where was this video before? I really needed it? Finally. Easy to understand.
@davidallsopp40304 жыл бұрын
the way of thinking about this in terms of what is "active" and what is "paused" in the stack is super helpful!
@mollyexten45084 жыл бұрын
Again, I love these visual examples of functions and the order in which they pop off the top of the stack. This is so helpful!
@lb97265 жыл бұрын
best visual explanation on recursion. I finally understand it. Thank you.
@beaumiffle3 жыл бұрын
If anyone needs a visual explanation; I've wrote it out below. I couldn't understand till I typed everything out, hopefully this helps someone too. FUNCTION: int fact(int n) { if (n == 1) return 1; else return n * fact(n-1); } int main(void) { printf("%i ", fact(5)); } EXPLANATION: int fact(int 5) { does (5 == 1)? no move to else statement; else return 5 * fact(5-1); [aka: return 5 * fact(4)] [what is fact(4)? let's find out] } int fact(int 4) { does (4 == 1)? no move to else statement; else return 4 * fact(4-1); [aka: return 4 * fact(3)] [what is fact(3)? let's find out] } int fact(int 3) { does (3 == 1)? no move to else statement; else return 3 * fact(3-1); [aka: return 3* fact(2)] [what is fact(2)? let's find out] } int fact(int 2) { does (2 == 1)? no move to else statement; else return 2 * fact(2-1); [aka: return 2 * fact(1)] [what is fact(1)? let's find out] } int fact(int 1) { does (1 == 1)? YES return 1; else statement not used } thus; fact(1) = 1 NOW BACK WE GO BACK! int fact(int 2) { else return 2 * fact(2-1); } return 2 * fact(1) what is fact(1)? 1 return [2 * 1] = 2 thus; fact(2) = 2 int fact(int 3) { else return 3 * fact(3-1); } return 3 * fact(2) what is fact(2)? 2 return [3 * 2] = 6 thus; fact(3) = 6 int fact(int 4) { else return 4 * fact(4-1); } return 4 * fact(3) what is fact(3)? 6 return [4 * 6] = 24 thus; fact(4) = 24 int fact(int 5) { else return 5 * fact(5-1); } return 5 * fact(4) what is fact(4)? 24 return [5 * 24] = 120 thus; fact(5) = 120 THEREFORE: printf("%i ", fact(5)); fact(5) = 120 print: 120
@ManhwaFlashbackForFun2 жыл бұрын
thanks for your explanation, I was confused in int fact 4 when he said 4 * 6
@stonehead665 ай бұрын
Thank you very much!
@satya-gs4sc5 ай бұрын
what about the space complexity ? can you please clarify
@mohdshkaib4 ай бұрын
thanks
@rdx864 жыл бұрын
I FINALLY understand recursion! After so many years! Thank you Doug and CS50!
@Hermeticoh1111 ай бұрын
Thank you, Doug! This is really helpful. I feel like my mind opens and all of a sudden I can understand how everything I've programmed this far works.
@laurisskraucis22476 жыл бұрын
Amazing! So grateful for your lessons Doug!
@JimMavrin Жыл бұрын
Thank you so much for the explanation! I was gonna bang my head against the wall cuz I simply coundn't understand how recursion works in the mario example David wrote. I spent probably more than an hour jumping through hoops trying to make GPT3 explain it, but I simply coudln't make sense of it. Now with 4:30 it all makes sense.
@andrewpratt38614 жыл бұрын
This brought a bunch of concepts together for me. Thanks, Doug!
@esjihn6 жыл бұрын
This and the numberphile explanation (recursion) are the best explanations on the web.
@DylanRobert14 жыл бұрын
Wow this is necessary for understanding recursion in week 3. Thank you for the information.
@LUITEN14 жыл бұрын
Thanks so much Doug!!! The quality of explanation on your videos are superb!!
@maxim5519 Жыл бұрын
The first explanation of recursions during all weeks in CS50 what I was able to understand 🙂 You definitely have to change Short name from Call Stacks to Recursions !!!
@omar241914 жыл бұрын
Best Recursion explanation I found in the web! Thanks Doug!
@Timo-Epis Жыл бұрын
I did understand recursion but I invented my own concept about it and you finally made the link between call stack and recursions when I was learning about call stacks. Thank you!
@aaronog4117 Жыл бұрын
I love you, Doug. Thank you for your explanation. It helps me to become a better person.
@saul6893 Жыл бұрын
You just explained magic to me!
@girijababu3 жыл бұрын
Awesome explanation...From the day I learned recursion in data structures and algorithm subject years back, I had this doubt. How the variables are retained in each call and the final output. Today this video gave me the answer...great relief
@mandyzhang95313 жыл бұрын
Such a great video! You explained the call stack so well! thank you Doug.
@patriotpatriotic38946 жыл бұрын
Thank you very much for this explanation and visualisation!
@Honestly_Marie4 жыл бұрын
Doug and his sexy codes lol, that is really all I remember from that recursion video.
@GrumpyStoic3 жыл бұрын
That bit was a classic. Love ya Doug you sexy code beast!
@fakrulotaku56558 ай бұрын
Not sexy, it was “seaxy”
@JuliaFeyst2 жыл бұрын
only after this video, I resolved tideman problem from Week 3 problem set. THANKS DOUG
@realEraldoNeto Жыл бұрын
Thank you very much, Doug! It was very intuitive and your explanation was precise and direct. :))
4 жыл бұрын
Doug you are an angel, thank you very much for the explanation.
@dixztube3 жыл бұрын
This was really great explanation. Very simple easy to follow
@iEatBass4 жыл бұрын
Doug you're the man, dude!
@bactran77994 жыл бұрын
thank you, it's so clear. Help me a lot to understand how recursion works and call stacks
@3du45d4 ай бұрын
Hey guys, just stumbled across this video, but please note that the call stack is not correct as shown from around 04:05. However, the "fact" function does go on the stack before printf!!!!.
@OfficialSombrero2 жыл бұрын
I finally understand recursion now. from the prior weeks lecture I was like well if its returning 1 when it gets to fact(1), how is it going to solve for the actual value. This should have been in the prior weeks lectures but i understand its a lot to fit in. Thanks
@learnerjetix9 ай бұрын
Finally understood why the return 1 in the base case does not end the program.
@luckydevil1601 Жыл бұрын
Incredible explanation! Thank you!
@KaungSettLwin-x7e7 ай бұрын
As a self though I have the problem understanding recursion. Some say "If you want to learn recursion you have to know recursion". Turns out that to understand recursion, we need to understand how call stacks work. And then the rest is become easy. Thanks
@gauravpdaksh3 жыл бұрын
CS50 is the best course. I wish I could goto Harvard.
@waltertaju4 жыл бұрын
Finally understood recursion properly with this explanation!
@TheOctavaTube2 жыл бұрын
Great presentation. Minor point on this factorial example, it seems that the call stack should also include the 'n' value , n*fact(n-1) fact(1) 2*fact(1) ---- fact(1) 3*fact(2)---- fact(2) 4*fact(3)---- fact(3) 5*fact(4)----fact(4) fact(5) ------fact(5) print() -----print() main() ---- main()
@AyushDhingra4 жыл бұрын
If we give a much bigger value to fact(), will there be a case when the memory contains so much paused fact() frames that the program crashes? By the way, this is a very helpful video.
@braceleerohith4 жыл бұрын
yup a stack overflow occurs.
@nannubedi77735 жыл бұрын
Very nice sir!! This indeed helped a lot. Thank you so much.
@kieranbarker1902 Жыл бұрын
This is fantastic, thank you!
@qq-mf9pw2 жыл бұрын
Very clear explanation! Small nitpick, printf() should not have a stack frame in this example. Arguments are evaluated first (building on top of the main() frame) and all fact() frames will be removed from the stack before calling printf(). But this is not a big deal at an introductory level :)
@Ryan-xb1ry2 жыл бұрын
This is so great. Thank you
@raztubes Жыл бұрын
Pedantic point, but printf is called AFTER the fact completes. That parameter has to be evaluated before the call enters printf. Then again, been a while since i've done C, so i might be wrong.
@nowak932 жыл бұрын
Got a little while before I figured out recursion last week, the clue was what I returned up the stack.
@AnujTiwari-g8o Жыл бұрын
kya baat hai guru maja aa gaya . waah waah waah waah .
@maboy19944 жыл бұрын
These folks are brilliant! I wonder why this video is excluded from the shorts in week 3?
@xinguangmingwo23434 жыл бұрын
Thank you very much. It saves me a lot of time.
@kaliomar59885 жыл бұрын
great explanation Mr. Lloyd
@nikbobrov50454 жыл бұрын
I never saw this kind of explanation. Now recursion is transparent to me like spring water.
@philteng7605 жыл бұрын
Thank you, great work, life saver! Thank you so much!
@alialparslan22883 жыл бұрын
Until this short, I thought that I understand what is recursion. Now I understand well.
@mushahidhussain15166 жыл бұрын
You're amazing sir.
@asrcool44162 жыл бұрын
Very nice, easy to understand explanation! Thank you!
@NeKe4rl5 жыл бұрын
Very clear, thank you
@gregoriomiranda69172 ай бұрын
Now I understand the Mr. Meeseeks episode of Rick and Morty
@meghnasingh43964 жыл бұрын
Very well put ! Thank you very much !
@jsteezeful4 жыл бұрын
Excellent explanation! Thank you.
@flyte98443 жыл бұрын
best video on recursion ! thx !
@MrSkyydude Жыл бұрын
Really good explanations.
@Ronaldoforever33 жыл бұрын
Explained in very easy way tysm.
@braceleerohith4 жыл бұрын
so that's why the return type is so important ...OMG..now everything makes sense.
@Liam-10 Жыл бұрын
Thank you ❤️
@jessif.2 жыл бұрын
Thank you.
@jeroenkoffeman94024 жыл бұрын
great visual explanation!
@NKLGaming013 ай бұрын
Thank you very much
@ian_senior4 жыл бұрын
protip: 3:34 you can remove the else and get the same results. like so... int fact(int n) { if (n == 1) return 1; return n * fact(n - 1); }
@freshlemon1014 жыл бұрын
Or, int fact(int n) { return n == 0 ? 1 : n * fact(n - 1); }
@exnihilonihilfit63164 жыл бұрын
@@freshlemon101 You mean "n == 1", not 0.
@freshlemon1014 жыл бұрын
@@exnihilonihilfit6316 I included 0 factorial
@exnihilonihilfit63162 жыл бұрын
You're correct; your version works for factorial 1 and factorial 0. Good job. They completely ignored factorial 0 in this exercise - I guess because they didn't want to deal with explaining why 0 factorial is 1. :) (and it took me over a year to respond) :]
@amaldev41504 жыл бұрын
why isn't this video on lect 3? this gave me so much hard time ! i couldn't understand recursion, and i was so stressed for not understanding it! please put this on lect 3
@Avalanchanime2 жыл бұрын
Thank you
@subvertedhope4 жыл бұрын
Wish someone would have just told me that functions work like Magic: the Gathering cards from the start.
@stoicstone521 Жыл бұрын
understood. thank you
@itspurelypassionate4 жыл бұрын
Oh.. now i understand recursion.Thanks
@abd-elrahmanmohamed98396 жыл бұрын
Well explained , Thanks !
@PareshRadadiya223 жыл бұрын
Doug Lloyd is awesome man. 👨🏼🏫
@codeative6 жыл бұрын
So clear, thank you :)
@pfever2 жыл бұрын
3:37 this is technically wrong, before printf() can be called fact(5) must be evaluated.
@sia35403 жыл бұрын
How do I troubleshoot Windows 10 error "new guard page for the stack cannot be created"? I know nothing about programing!
@a.rangarajanrajan32395 жыл бұрын
Thanks for grate vedio. Who will handle this frame creation if we don't have is, For example general embedded systems
@maxfeliciello47616 жыл бұрын
Awesome, Thanks!!
@ridoychandradey44462 жыл бұрын
It's all clear now how recursion actually work under the hood. Thank you Doug Lloyd.
main calls fact(5) which returns then main calls printf(string,result of calling fact(5))
@AFStimo25 жыл бұрын
yep, this comment should be on top - fact is called first and printf is called only when all fact calls are done, otherwise there is no parameter to pass to printf
@markmathias92404 жыл бұрын
@@AFStimo2 The code is written such that while the fact function is defined, it doesn't come into play until after "int main(void)"
@max500k5 жыл бұрын
Great video, very clear explanation, thanks!
@GOODBOY-vt1cf4 жыл бұрын
thank you!
@yonaxl4 жыл бұрын
So is this a good way or not a good way to program?
@anuarsadykov97075 жыл бұрын
If I had fact(1 000 000 000), would it be reasonable to iterate values in a loop instead of using recursion? If I use recursion, functions would stack up billion times and occupy a lot of memory, while loop operates inside main(). Which one occupies more space in memory recursion or iteration in a loop method? Which one is faster?
@kaliomar59885 жыл бұрын
loops is faster and it occupy less space in memory than recursion.
@balupabbi4 жыл бұрын
how does call stack look like with tail recursion and why does it use less stack memory compared to regular recursion?
@nshk71634 жыл бұрын
I understood recursion functions but still don't fully understand how to implement it instead of an iterative one
@asashish9056 жыл бұрын
I have a question, is this applicable on all callbacks? Or only Ajax calls and set Timeout?
@rtrs54132 жыл бұрын
i was just curious how you first learned this??
@김태우-o5h3f Жыл бұрын
very helpful!!
@arielspalter74254 жыл бұрын
Excellent explanation! Where are all the likes?
@stefaneew2 жыл бұрын
You are my hero. I waned to die because of not understanding recursion on the week 5