Call Stacks - CS50 Shorts

Sdílet
Vložit
  • čas přidán 24. 01. 2018
  • ***
    This is CS50, Harvard University's introduction to the intellectual enterprises of computer science and the art of programming.
    ***
    HOW TO SUBSCRIBE
    czcams.com/users/subscription_c...
    HOW TO TAKE CS50
    edX: cs50.edx.org/
    Harvard Extension School: cs50.harvard.edu/extension
    Harvard Summer School: cs50.harvard.edu/summer
    OpenCourseWare: cs50.harvard.edu/x
    HOW TO JOIN CS50 COMMUNITIES
    Discord: / discord
    Ed: cs50.harvard.edu/x/ed
    Facebook Group: / cs50
    Faceboook Page: / cs50
    GitHub: github.com/cs50
    Gitter: gitter.im/cs50/x
    Instagram: / cs50
    LinkedIn Group: / 7437240
    LinkedIn Page: / cs50
    Reddit: / cs50
    Quora: www.quora.com/topic/CS50
    Slack: cs50.edx.org/slack
    Snapchat: / cs50
    Twitter: / cs50
    CZcams: / cs50
    HOW TO FOLLOW DAVID J. MALAN
    Facebook: / dmalan
    GitHub: github.com/dmalan
    Instagram: / davidjmalan
    LinkedIn: / malan
    Quora: www.quora.com/profile/David-J...
    Twitter: / davidjmalan
    ***
    CS50 SHOP
    cs50.harvardshop.com/
    ***
    LICENSE
    CC BY-NC-SA 4.0
    Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International Public License
    creativecommons.org/licenses/...
    David J. Malan
    cs.harvard.edu/malan
    malan@harvard.edu

Komentáře • 134

  • @realVertiqo
    @realVertiqo Před 4 lety +233

    Seriously Guys put this video into weeks 3 shorts as an addon to the recursion one, this explains it so good!

    • @marketsareopenmao
      @marketsareopenmao Před 3 lety +24

      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.

    • @GrumpyStoic
      @GrumpyStoic Před 3 lety +2

      @@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.

    • @marketsareopenmao
      @marketsareopenmao Před 3 lety +2

      @@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.

    • @gonzaotc
      @gonzaotc Před 2 lety

      Good advice.

    • @djm_852
      @djm_852 Před 2 lety +5

      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.

  • @maximbrykov
    @maximbrykov Před 6 lety +176

    Finally, I got recursion and stack frames after your explanation. Thank you Doug.

  • @thewallstreetjournal5675
    @thewallstreetjournal5675 Před 4 lety +54

    Finally I see what is meant by a return.
    It makes no sense without the concept of the stack frame.

  • @marnierogers3931
    @marnierogers3931 Před 2 lety +11

    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!!

  • @JackyVSO
    @JackyVSO Před 4 lety +31

    This was great. I was confused after the Week 3 lecture and still confused after the Recursion video but now I get this principle.

  • @ricklangston8895
    @ricklangston8895 Před 5 lety +32

    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.

  • @albinsopaj
    @albinsopaj Před 4 lety +15

    Where was this video before? I really needed it? Finally. Easy to understand.

  • @shreephadke3679
    @shreephadke3679 Před 4 lety +13

    This video was incredibly easy to understand and the concept was very eloquently taught. Thank you, Doug!

  • @laurisskraucis2247
    @laurisskraucis2247 Před 5 lety +6

    Amazing! So grateful for your lessons Doug!

  • @mollyexten4508
    @mollyexten4508 Před 3 lety +2

    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!

  • @davidallsopp4030
    @davidallsopp4030 Před 4 lety +1

    the way of thinking about this in terms of what is "active" and what is "paused" in the stack is super helpful!

  • @andrewpratt3861
    @andrewpratt3861 Před 3 lety +1

    This brought a bunch of concepts together for me. Thanks, Doug!

  • @lb9726
    @lb9726 Před 4 lety +9

    best visual explanation on recursion. I finally understand it. Thank you.

  • @LUITEN1
    @LUITEN1 Před 4 lety +1

    Thanks so much Doug!!! The quality of explanation on your videos are superb!!

  • @patriotpatriotic3894
    @patriotpatriotic3894 Před 5 lety +5

    Thank you very much for this explanation and visualisation!

  • @rdx86
    @rdx86 Před 3 lety +1

    I FINALLY understand recursion! After so many years! Thank you Doug and CS50!

  • @oussamatarhi6454
    @oussamatarhi6454 Před 3 lety +1

    Frankly, those Shorts are a knowledge treasure. Thank you David J. Malan, thank you Doug Lloyd, thank Harvard.

  • @DanielHermeticoh
    @DanielHermeticoh Před 4 měsíci

    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.

  • @esjihn
    @esjihn Před 6 lety +2

    This and the numberphile explanation (recursion) are the best explanations on the web.

  • @mandyzhang9531
    @mandyzhang9531 Před 2 lety +1

    Such a great video! You explained the call stack so well! thank you Doug.

  • @Honestly_Marie
    @Honestly_Marie Před 3 lety +5

    Doug and his sexy codes lol, that is really all I remember from that recursion video.

    • @GrumpyStoic
      @GrumpyStoic Před 3 lety

      That bit was a classic. Love ya Doug you sexy code beast!

    • @fakrulotaku5655
      @fakrulotaku5655 Před měsícem

      Not sexy, it was “seaxy”

  • @DylanRobert1
    @DylanRobert1 Před 4 lety +6

    Wow this is necessary for understanding recursion in week 3. Thank you for the information.

  • @omar24191
    @omar24191 Před 3 lety +1

    Best Recursion explanation I found in the web! Thanks Doug!

  • @bactran7799
    @bactran7799 Před 3 lety

    thank you, it's so clear. Help me a lot to understand how recursion works and call stacks

  • @Timo-Epis
    @Timo-Epis Před rokem

    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!

  •  Před 3 lety

    Doug you are an angel, thank you very much for the explanation.

  • @aaronog4117
    @aaronog4117 Před 8 měsíci

    I love you, Doug. Thank you for your explanation. It helps me to become a better person.

  • @girijababu
    @girijababu Před 2 lety

    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

  • @luckydevil1601
    @luckydevil1601 Před 8 měsíci

    Incredible explanation! Thank you!

  • @JuliaFeyst
    @JuliaFeyst Před 2 lety

    only after this video, I resolved tideman problem from Week 3 problem set. THANKS DOUG

  • @iEatBass
    @iEatBass Před 4 lety +1

    Doug you're the man, dude!

  • @kieranbarker1902
    @kieranbarker1902 Před 11 měsíci

    This is fantastic, thank you!

  • @Ryan-xb1ry
    @Ryan-xb1ry Před 2 lety

    This is so great. Thank you

  • @mushahidhussain1516
    @mushahidhussain1516 Před 5 lety +1

    You're amazing sir.

  • @philteng760
    @philteng760 Před 4 lety

    Thank you, great work, life saver! Thank you so much!

  • @davidanthonyburton2253

    TY! I feel this is especially important

  • @eraldoneto6119
    @eraldoneto6119 Před 7 měsíci

    Thank you very much, Doug! It was very intuitive and your explanation was precise and direct. :))

  • @saul6893
    @saul6893 Před 11 měsíci +1

    You just explained magic to me!

  • @kaliomar5988
    @kaliomar5988 Před 5 lety +1

    great explanation Mr. Lloyd

  • @nannubedi7773
    @nannubedi7773 Před 4 lety +3

    Very nice sir!! This indeed helped a lot. Thank you so much.

  • @waltertaju
    @waltertaju Před 3 lety

    Finally understood recursion properly with this explanation!

  • @dixztube
    @dixztube Před 2 lety +1

    This was really great explanation. Very simple easy to follow

  • @user-fg5so7dl2n
    @user-fg5so7dl2n Před rokem

    kya baat hai guru maja aa gaya . waah waah waah waah .

  • @learnerjetix
    @learnerjetix Před 2 měsíci

    Finally understood why the return 1 in the base case does not end the program.

  • @meghnasingh4396
    @meghnasingh4396 Před 4 lety

    Very well put !
    Thank you very much !

  • @jeroenkoffeman9402
    @jeroenkoffeman9402 Před 4 lety

    great visual explanation!

  • @jsteezeful
    @jsteezeful Před 4 lety

    Excellent explanation! Thank you.

  • @maxim5519
    @maxim5519 Před rokem +1

    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 !!!

  • @NeKe4rl
    @NeKe4rl Před 4 lety +1

    Very clear, thank you

  • @xinguangmingwo2343
    @xinguangmingwo2343 Před 3 lety

    Thank you very much. It saves me a lot of time.

  • @codeative
    @codeative Před 5 lety +1

    So clear, thank you :)

  • @flyte9844
    @flyte9844 Před 2 lety

    best video on recursion ! thx !

  • @abd-elrahmanmohamed9839
    @abd-elrahmanmohamed9839 Před 5 lety +1

    Well explained , Thanks !

  • @maxfeliciello4761
    @maxfeliciello4761 Před 5 lety +1

    Awesome, Thanks!!

  • @OfficialSombrero
    @OfficialSombrero Před rokem

    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

  • @asrcool4416
    @asrcool4416 Před 2 lety

    Very nice, easy to understand explanation! Thank you!

  • @MrSkyydude
    @MrSkyydude Před rokem

    Really good explanations.

  • @Liam-10
    @Liam-10 Před 10 měsíci

    Thank you ❤️

  • @jessif.
    @jessif. Před 2 lety

    Thank you.

  • @user-yp1gx5rx3h
    @user-yp1gx5rx3h Před 28 dny

    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

  • @user-no1ey2pt1j
    @user-no1ey2pt1j Před 8 měsíci

    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.

  • @Avalanchanime
    @Avalanchanime Před 2 lety

    Thank you

  • @gauravpdaksh
    @gauravpdaksh Před 2 lety +2

    CS50 is the best course. I wish I could goto Harvard.

  • @beaumiffle
    @beaumiffle Před 2 lety +10

    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

    • @sleepygodyt6860
      @sleepygodyt6860 Před rokem

      thanks for your explanation, I was confused in int fact 4 when he said 4 * 6

  • @karthikgaddam4560
    @karthikgaddam4560 Před 3 lety

    Explained in very easy way tysm.

  • @stoicstone521
    @stoicstone521 Před 10 měsíci

    understood. thank you

  • @kakashisenpai99
    @kakashisenpai99 Před 4 lety

    Thanks a ton !

  • @GOODBOY-vt1cf
    @GOODBOY-vt1cf Před 4 lety

    thank you!

  • @max500k
    @max500k Před 4 lety

    Great video, very clear explanation, thanks!

  • @nowak93
    @nowak93 Před 2 lety

    Got a little while before I figured out recursion last week, the clue was what I returned up the stack.

  • @TheOctavaTube
    @TheOctavaTube Před rokem

    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()

  • @qq-mf9pw
    @qq-mf9pw Před 2 lety +1

    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 :)

  • @itspurelypassionate
    @itspurelypassionate Před 3 lety

    Oh.. now i understand recursion.Thanks

  • @maboy1994
    @maboy1994 Před 3 lety +1

    These folks are brilliant! I wonder why this video is excluded from the shorts in week 3?

  • @nikbobrov5045
    @nikbobrov5045 Před 3 lety

    I never saw this kind of explanation. Now recursion is transparent to me like spring water.

  • @user-sc4qq3sc5l
    @user-sc4qq3sc5l Před 7 měsíci

    very helpful!!

  • @braceleerohith
    @braceleerohith Před 3 lety

    so that's why the return type is so important ...OMG..now everything makes sense.

  • @alialparslan2288
    @alialparslan2288 Před 2 lety

    Until this short, I thought that I understand what is recursion. Now I understand well.

  • @DaniloSilva-pl3sq
    @DaniloSilva-pl3sq Před 3 lety

    GOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOD

  • @raztubes
    @raztubes Před 5 měsíci

    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.

  • @PareshRadadiya22
    @PareshRadadiya22 Před 3 lety

    Doug Lloyd is awesome man. 👨🏼‍🏫

  • @a.rangarajanrajan3239
    @a.rangarajanrajan3239 Před 4 lety

    Thanks for grate vedio.
    Who will handle this frame creation if we don't have is,
    For example general embedded systems

  • @ian_senior
    @ian_senior Před 4 lety +7

    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);
    }

    • @freshlemon101
      @freshlemon101 Před 3 lety +1

      Or,
      int fact(int n) {
      return n == 0 ? 1 : n * fact(n - 1);
      }

    • @exnihilonihilfit6316
      @exnihilonihilfit6316 Před 3 lety +3

      @@freshlemon101 You mean "n == 1", not 0.

    • @freshlemon101
      @freshlemon101 Před 3 lety +1

      @@exnihilonihilfit6316
      I included 0 factorial

    • @exnihilonihilfit6316
      @exnihilonihilfit6316 Před 2 lety

      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) :]

  • @hneto2101
    @hneto2101 Před 4 lety

    perfect

  • @subvertedhope
    @subvertedhope Před 3 lety +2

    Wish someone would have just told me that functions work like Magic: the Gathering cards from the start.

  • @AyushDhingra
    @AyushDhingra Před 4 lety +13

    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.

  • @MichaelFoley64
    @MichaelFoley64 Před 6 lety +10

    main calls fact(5) which returns then main calls printf(string,result of calling fact(5))

    • @AFStimo2
      @AFStimo2 Před 4 lety +3

      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

    • @markmathias9240
      @markmathias9240 Před 4 lety

      @@AFStimo2 The code is written such that while the fact function is defined, it doesn't come into play until after "int main(void)"

  • @asashish905
    @asashish905 Před 5 lety

    I have a question, is this applicable on all callbacks? Or only Ajax calls and set Timeout?

  • @amaldev4150
    @amaldev4150 Před 3 lety

    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

  • @balupabbi
    @balupabbi Před 3 lety

    how does call stack look like with tail recursion and why does it use less stack memory compared to regular recursion?

  • @nshk7163
    @nshk7163 Před 4 lety

    I understood recursion functions but still don't fully understand how to implement it instead of an iterative one

  • @arielspalter7425
    @arielspalter7425 Před 3 lety

    Excellent explanation! Where are all the likes?

  • @anuarsadykov9707
    @anuarsadykov9707 Před 5 lety

    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?

    • @kaliomar5988
      @kaliomar5988 Před 5 lety

      loops is faster and it occupy less space in memory than recursion.

  • @krimskiymist
    @krimskiymist Před rokem

    You are my hero. I waned to die because of not understanding recursion on the week 5

  • @yonaxl
    @yonaxl Před 3 lety

    So is this a good way or not a good way to program?

  • @sia3540
    @sia3540 Před 2 lety

    How do I troubleshoot Windows 10 error "new guard page for the stack cannot be created"? I know nothing about programing!

  • @ridoychandradey4446
    @ridoychandradey4446 Před 2 lety

    It's all clear now how recursion actually work under the hood. Thank you Doug Lloyd.

  • @abhishekgautam1063
    @abhishekgautam1063 Před 4 lety

    What if a function just call itself and does nothing. After how much time the recursive stack will be full and what is the time after which the prrogram will terminate. please help!

    • @weyauh4338
      @weyauh4338 Před 4 lety +3

      There's not a definite answer to your question. The program will terminate when the function called itself so many times that the stack is full. That is known as a Stack Overflow.

  • @davidanthonyburton2253

    pardon me It's like talking to our processor or is it the ram memory? but please ignore my comment please. just ty for Ur vid 👍!

  • @replakcan
    @replakcan Před 2 lety

    string s'den char* c'ye geçişten sonra yaşadığım ikinci büyük aydınlanmam: recursion'ı anlamamı sağlayan call stack mevzusu.