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 ---
I am very impressed with these tutorials, you do an excellent job getting right to the point for each topic.
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!!!
Hi Team, How TAILREC internally works ?
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
check decompiled code for tailrec function. You will see while(true) loop instead of recursion which means tailrec function uses single Stack Frame
uow this content is really good. Thanks my friend.
can you explain the code of Fibonacci series
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)
umm...... Okay, tailrec is cool and all but what does it do differently to be able to do this
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.)
Gud teacher till now this video clear audio everything best
Thanks a lot
Sir my question is why you use BigInteger("1") and BigInteger("0") why we can't send valuse 1,0 simple
The reason is because those would be integer types, and Big Integer type can generate a much bigger number.
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.
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
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
@@abhishekmadan5075 thank you for this example
that was a good points sir
Great video
I think you forgot to menton that "The recursive call must be the very last call of the method"
Make videos on courotines kotlin
right