Recursion | C Programming Tutorial

Поділитися
Вставка
  • Опубліковано 15 жов 2024

КОМЕНТАРІ • 44

  • @SteezKaytranada
    @SteezKaytranada 24 дні тому

    it's hard to find a free C course that deeps dive with C feautures. This is very helpful thanks!

  • @BLACK-AUTUMN-MAGICK
    @BLACK-AUTUMN-MAGICK Рік тому +34

    Thank you for speaking CLEAR English... Thank you for enunciating your words... 90% of programming videos are either in a foreign language, or done by someone with such a heavy accent that doesn't bother themselves to at least TRY to enunciate so as to be understood! Subscribed!

    • @PortfolioCourses
      @PortfolioCourses  Рік тому +3

      You're welcome, I try my best to speak clearly! 🙂

    • @aliyuibrahim7076
      @aliyuibrahim7076 Рік тому

      😂😂😂

    • @samasf4154
      @samasf4154 Рік тому +5

      racist much

    • @randomawesomeness852
      @randomawesomeness852 Рік тому +1

      ​@@samasf4154 naah i feel him , i am a foreign my self so i only understand clear English and it is hard for me to understand Indians talk English if the video is 2 hours then it would take me 4 hours to understand

    • @Mydrac
      @Mydrac 6 місяців тому

      @@randomawesomeness852 It is very interesting, why not just go and see other video in "CLEAR English".

  • @Pearson2356
    @Pearson2356 2 роки тому +12

    Very understandable than our teachers' explanation, thanks.

  • @jackschool1369
    @jackschool1369 Рік тому

    Very well explained I finally understand. The example explanation at 5:29 was the best because most other people skip that and go to the abstract explanation. I just had a hard time understanding how the function would loop through a chain of multiplication. This makes more sense, each function is unpacked into a smaller function until it reaches the base condition.

  • @AnalogDude_
    @AnalogDude_ 8 місяців тому

    i can't measure if the memory increases, but it works when making factorial function static.
    Done this before and "31" is the biggest number you can do even when making all the variables of type: long.
    QT compiler doesn't allow the last line in the factorial function, return + code.

  • @freelance-writer
    @freelance-writer 2 місяці тому +1

    Great explanation. Although I'd really love to see a video on recursion that ISN'T about factorials. 🙃

    • @PortfolioCourses
      @PortfolioCourses  2 місяці тому

      If you search “recursion” on this channel page you’ll find a bunch of other recursion examples. :-)

  • @mostafamoradi2051
    @mostafamoradi2051 2 роки тому +4

    nice explanation. but what are does issues? i just tested n=100 with this function, and the computer i think lost itself:)
    it would be great if you could explain more about this function. well I am amateur but others say this function is so useful, just wanna find out more about this one at least. thanks for your efforts:).

    • @PortfolioCourses
      @PortfolioCourses  2 роки тому +4

      The problem with n=100 is that factorial grows so quickly that the numbers become larger than our computer can deal with using the regular way it represents numbers: en.wikipedia.org/wiki/Factorial. There is also such an explosion of function calls that our computer will not have enough memory available for the process to handle the situation, as each function call also takes up a form of memory as well.

    • @snipzmattio5887
      @snipzmattio5887 2 роки тому +4

      This way it works:
      #include
      long factorial(int n)
      {
      if (n == 0) return 1;
      else
      return(n * factorial(n-1));
      }
      int main()
      {
      int number;
      long fact;
      printf("Enter a number: ");
      scanf("%d", &number);
      fact = factorial(number);
      printf("Factorial of %d is %ld
      ", number, fact);
      return 0;
      }

    • @snipzmattio5887
      @snipzmattio5887 2 роки тому +3

      you just have to play with the data types to so that C doesnt think you want to kill your ram

  • @lradhakrishnarao902
    @lradhakrishnarao902 2 роки тому +2

    Another thing to note is that, in recursion, variables are strictly local to the function, and there are no need to use loop and change the state of the variables. With iterative approach, we need to keep on changing the state of the variables, which becomes very tough to manage.
    However, recursive programs can be cumbersome, if the algorithm has exponential time complexity.

    • @PortfolioCourses
      @PortfolioCourses  2 роки тому +1

      Definitely... and with recursion we have a risk of stack overflow as well: en.wikipedia.org/wiki/Stack_overflow.

    • @alvarobarboza5730
      @alvarobarboza5730 Рік тому

      @@PortfolioCourses just watched your video concerning stack overflow, very very clear and precise. Thank you.
      The heads and tails recursion video is next. Thank you !!

  • @dubstepbanane6277
    @dubstepbanane6277 4 місяці тому

    wouldnt the break case have to be if (n == 0) return 1
    Because 0! = 1

    • @PortfolioCourses
      @PortfolioCourses  4 місяці тому

      If we want to support the convention that the factorial of 0 is 1 then yes, this function was made as a simple example of recursion assuming we’re finding the factorial of positive integers. :-)

  • @goktugparlak1618
    @goktugparlak1618 11 місяців тому

    Sir could you explain how the Return statement work's why the function did not return just 1 because when the n ==1 it returns 1 and exit's the function.But it return's all function call values.

    • @PortfolioCourses
      @PortfolioCourses  11 місяців тому

      I tried explaining this in the video, but with a recursion we have a function that calls itself. So when the function returns 1 when n==1, that return value is being returned to the *previous* function call where it is then used, and so on, until we get back to the original/first function call. :-)

    • @goktugparlak1618
      @goktugparlak1618 11 місяців тому

      Thanks a lot I understand@@PortfolioCourses

    • @PortfolioCourses
      @PortfolioCourses  11 місяців тому

      Excellent! :-)

  • @qneqne8440
    @qneqne8440 Рік тому

    Sometimes I get an error, "all paths through this function will call itself" and when I don't get that error, the program runs on an infinite loop. Any comments?

    • @PortfolioCourses
      @PortfolioCourses  Рік тому +2

      It sounds like something might be 'off' with your if-statement that's causing the compiler to think that, and then when you make a modification, it's still 'off' but it turns into 'infinite recursion'. The code for the video is here: github.com/portfoliocourses/c-example-code/blob/main/recursion.c. Maybe try that code and see if it works? :-)

    • @qneqne8440
      @qneqne8440 Рік тому

      @@PortfolioCourses It seems that when I used an "if" statement, it worked fine but when I used "while" it led to an infinite loop. Not sure if that's really the case or if there were errors in my implementations but thank you for your kind response!

    • @PortfolioCourses
      @PortfolioCourses  Рік тому

      @@qneqne8440 Oh OK that makes sense, yes using a while could definitely lead to an infinite loop because a while loop repeats code but an if-statement makes a decision about which code to execute (or not). 🙂

  • @aliyuibrahim7076
    @aliyuibrahim7076 Рік тому

    what does int fact = 1; mean pls?

    • @PortfolioCourses
      @PortfolioCourses  Рік тому +1

      "int fact = 1;" will declare a variable with the name "fact" that can store values of type "int" (i.e. integer numbers like -1,2,45, but not numbers with decimal places like 2.5). The "= 1" portion uses the assignment operator "=" to assign the value 1 to the variable... i.e. fact will store the value 1. :-)

  • @alvarobarboza5730
    @alvarobarboza5730 Рік тому

    thank you sir !

  • @Imperialcodex1
    @Imperialcodex1 Рік тому

    Thank you so so much

  • @mongraal2272
    @mongraal2272 2 роки тому +1

    sir but what means if(n == 1) return 1;
    Why 1? and not 0;

    • @PortfolioCourses
      @PortfolioCourses  2 роки тому +3

      It's because !0 is defined as 1: en.wikipedia.org/wiki/Factorial. That's really the only reason, factorial of 0 is 1, so for 0, which is also our base case, we return 1.

    • @zoquevil4792
      @zoquevil4792 2 роки тому +1

      Just keep in mind that "1" is the neutral element of the multiplication ....
      zero factorial is funny, you can just ask " how many ways we can arange "one thing" , the answer is just one way (♣).... " how many ways we can arange "nothing" -- > just "one way"
      two things (♣,♥)(without repeating) just ----> (1 * 2): [♣-♥//♥,♣],
      three things (♦,♣.♥): [♦♣♥//♦♥♣//♥♦♣//♥♣♦//♣♦♥//♣♥♦]---- > ( 1 * 2 * 3)
      etc ...

  • @nezbrun872
    @nezbrun872 4 місяці тому

    Recursion: the most overrated wet dream of computer science academics.
    In practice, it's almost always better in terms of performance, resources, readability and maintainability to use iterative methods.

    • @PortfolioCourses
      @PortfolioCourses  4 місяці тому

      Hahaha 'wet dream of computer science academics'. :-P I hear you, but recursion actually does have *some* practical use cases with certain data structures, it's a best tool for the job situation where it's rarely the best tool for the job. We teach recursion in 'academia' mostly as a way of teaching how to think computationally and solve problems. To me the real 'wet dreams of computer science academics' are things like Agda: en.wikipedia.org/wiki/Agda_(programming_language).