Kotlin TAILREC Function example. Use Recursion in Optimised way. Kotlin Android Tutorial #7.9

Поділитися
Вставка
  • Опубліковано 15 жов 2024
  • Access 7000+ courses for 15 days FREE: pluralsight.px...
    Kotlin Android Tutorial. Unlike Java, C++ or C, in Kotlin there exists TAILREC functions that implements recursion without affecting the Stack Memory of your application. So the exception such as STACKOVERFLOW exception is avoided.
    Source code files : bit.ly/30ogUMG
    Next Video : • Kotlin Class Definitio...
    Previous Video : • Kotlin INFIX FUNCTION.... .
    .
    Please donate and support my work
    (If you think my free tutorials are better than paid ones :)
    Patreon: bit.ly/patreon...
    Paypal/Payoneer: sriyank123@gmail.com
    UPI (only for India): smartherd@okaxis
    :: If you want to develop a website or a mobile app, email me your requirement at sriyank.siddhartha@gmail.com :: Free demos provided beforehand ::
    Access my premium courses: bit.ly/sriyank...
    Free Programming courses:
    Ruby Programming: bit.ly/smyt-r
    Dart Programming: bit.ly/smyt-d
    Kotlin Programming: bit.ly/smyt-k
    Java Programming: bit.ly/smyt-j
    Kotlin Coroutines: bit.ly/smyt-coru
    Free Flutter course:
    Flutter App Development: bit.ly/2Rg7EFR
    Free Android courses:
    Android using Kotlin: bit.ly/smyt-ka
    Android using Java: bit.ly/smyt-ja
    Android Material Design: bit.ly/2SMJqU6
    Android Jetpack Architecture: bit.ly/yt-j
    Android Multiple Screen Support: bit.ly/smyt-mss
    Android Retrofit: bit.ly/2Ee6GHn
    More free programming courses:
    bit.ly/smy-list
    Check out my website:
    bit.ly/smartherd
    Let's get in touch! [Sriyank Siddhartha]
    LinkedIn: bit.ly/sriyank...
    Facebook: bit.ly/smarthe...
    Instagram: bit.ly/sriyank...
    Twitter: bit.ly/sriyank...
    Github: bit.ly/smarthe...
    --- Thank you for your love and support ---

КОМЕНТАРІ • 23

  • @sicariussss
    @sicariussss 4 роки тому +4

    I am very impressed with these tutorials, you do an excellent job getting right to the point for each topic.

  • @gauravvats5808
    @gauravvats5808 4 роки тому

    Nice Description in all your videos and for a java developer it is very easy to write code using Kotlin by following your videos.success!!!

  • @life2.091
    @life2.091 4 роки тому +3

    Hi Team, How TAILREC internally works ?

    • @saravananm2280
      @saravananm2280 3 роки тому

      In normal recursion, you perform all recursive calls first, and calculate the result from return values at last (as show in the above example). Hence, you don't get result until all recursive calls are made.
      In tail recursion, calculations are performed first, then recursive calls are executed (the recursive call passes the result of your current step to the next recursive call). This makes the recursive call equivalent to looping, and avoids the risk of stack overflow.
      soruce : www.programiz.com/kotlin-programming/recursion

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

      check decompiled code for tailrec function. You will see while(true) loop instead of recursion which means tailrec function uses single Stack Frame

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

    uow this content is really good. Thanks my friend.

  • @prashantsawarwadkar5127
    @prashantsawarwadkar5127 6 років тому +5

    can you explain the code of Fibonacci series

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

      n is getting decremented on each iteration and the a and b values are getting replaced. Just remember that the new values are being passed as arguments to the next generation and NOT variables which are immediately updated
      Imagine you run it with n = 5
      first loop: n = 4, upcoming a = 1 (itself + b which is 0), b = current a ( which is currently 1)
      second loop: n = 3, upcoming a = 2 (1 + 1), b = current a (which is 1)
      third loop: n = 2, upcoming a = 3 (2 + 1), b = current a (which is 2)
      fourth loop: n = 1, upcoming a = 5 (3 +2), b = current a(which is 3)
      fifth loop: n = 0, upcoming a = 8 (5 + 3), b = current a (which is 5)
      sixth loop: IF exception met b/c n == 0, return value of b (which is currently 5)

  • @AyushGupta-wn6zd
    @AyushGupta-wn6zd 4 роки тому

    umm...... Okay, tailrec is cool and all but what does it do differently to be able to do this

  • @michaelscofield1970
    @michaelscofield1970 4 роки тому

    Tail recursion optimizations happen automatically in C++ if the compiler sees the function fit for the optimization. (And it's very good at detecting it.)

  • @SpotTheError1
    @SpotTheError1 6 років тому

    Gud teacher till now this video clear audio everything best

  • @tcomedyvideos8199
    @tcomedyvideos8199 5 років тому +1

    Sir my question is why you use BigInteger("1") and BigInteger("0") why we can't send valuse 1,0 simple

    • @coolchri3
      @coolchri3 5 років тому

      The reason is because those would be integer types, and Big Integer type can generate a much bigger number.

    • @VivekYadav-ds8oz
      @VivekYadav-ds8oz 4 роки тому

      You could, there's no such restriction. He used BigInteger because the user can supply bigger numbers like 21093129009213 and 210912490812940. For simplicity, he used 1 and 0.

  • @Kyatt
    @Kyatt 5 років тому

    It's worth noting that tailrec only works with tail-recursion (that is different kind of recursion). Tried earlier with a "normal" recursive function and it didn't work

    • @abhishekmadan5075
      @abhishekmadan5075 5 років тому +2

      One of the conditions for the function to qualify for "tailrec" is that the last call of the function should be a recursion. ie if you consider the below example for finding factorial:
      fun findFactorial(num: Long): Long {
      return if (num

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

      @@abhishekmadan5075 thank you for this example

  • @ahmadpoorgholam8964
    @ahmadpoorgholam8964 6 років тому +1

    that was a good points sir

  • @ranganafernando511
    @ranganafernando511 5 років тому

    Great video

  • @chetanshetty3739
    @chetanshetty3739 3 роки тому

    I think you forgot to menton that "The recursive call must be the very last call of the method"

  • @aviksutar
    @aviksutar 5 років тому

    Make videos on courotines kotlin

  • @way_to_infinite1357
    @way_to_infinite1357 5 років тому

    right