Is recursive functions frequently uses? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 25

Is recursive functions frequently uses?

25th Apr 2021, 2:25 PM
Javokhir Nematov
Javokhir Nematov - avatar
15 Answers
+ 11
No, it's a costly operation. Pros - * Not many, just number of lines of code is minimised. Cons - * Mostly unintuitive on first go (you have to create recursion tree to understand properly) * Exponential Time complexity (Not recommended) * Runtime Memory consumption (because it has to maintain the stack)(Not recommended at all) Overall it better to use quadratic time function if you can rather than recursion. But for learning you have to use both. Happy coding :)
26th Apr 2021, 5:18 AM
Gaurav Kumar Prabhat
Gaurav Kumar Prabhat - avatar
+ 8
A popular programmer on youtube said in 30 years he used recursion maybe twice on an actual project. With that said, I consider it the deadlift(complex weight lifting maneuver) of programming.. Suggesting to Learn the difficult stuff even if it isnt necessary. Also software development job interviews will require you know recursion. 🙏
26th Apr 2021, 7:18 PM
Richard Horvatich
Richard Horvatich - avatar
+ 6
Generally, recursive is not that good for some problems since it takes more time and space as well... That's why we go with Dynamic Programming.. But, it depends on the problem.
25th Apr 2021, 2:28 PM
sarada lakshmi
sarada lakshmi - avatar
+ 6
Sanjay Kamath Really you want to use recursion for Fibonacci which takes O(2^n) time and also O(n) recursion stack. On the other hand you can do it in O(n) time and O(1) space iteratively.
26th Apr 2021, 3:01 PM
Gaurav Kumar Prabhat
Gaurav Kumar Prabhat - avatar
+ 5
Sometime it is good to use recursion cause it make problem easier like in dfs graph traversal
25th Apr 2021, 2:39 PM
YUGRAJ
+ 5
Yeah I agree but if you do competitive programming then you will find that you will write the code for recursion almost half of the time.
25th Apr 2021, 2:52 PM
YUGRAJ
+ 5
Yes of course...like Fibonacci series....or I believe fractals like spiral galaxies or say, sunflower florets...
26th Apr 2021, 2:57 PM
Sanjay Kamath
Sanjay Kamath - avatar
+ 5
I am supposing you are using binary exponentiation for calculating a^b then you can also do iteratively with same time complexity and without recursion overhead (recursion stack). You can check this for reference. Always avoid recursion. It's my perspective and my limited experience. You can use recursion if you want. No body is stopping you from doing this. https://cp-algorithms.com/algebra/binary-exp.html
26th Apr 2021, 4:30 PM
Gaurav Kumar Prabhat
Gaurav Kumar Prabhat - avatar
+ 5
Martin Taylor not to mention, binary trees, when was your last time when you used a binary tree?
27th Apr 2021, 3:12 AM
Rellot's screwdriver
Rellot's screwdriver - avatar
+ 4
Salom qalaysz
26th Apr 2021, 12:04 PM
Anvar Muradaliyev
Anvar Muradaliyev - avatar
+ 4
Anvar Muradaliyev Men yaxshiman. Rahmat. Sizchi? Iltimos, ingliz tilida yozing, chunki men o'zbek tilidan ingliz tiliga google-ga tarjima qilishim kerak. 😁
26th Apr 2021, 2:55 PM
Gaurav Kumar Prabhat
Gaurav Kumar Prabhat - avatar
+ 4
Yeah but if you wanna calculate power(n, m) in which m can be 10^12 Then using recursion you can do it using recusrion in logn complexity And also if you wanna do dfs traversal then it needs a stack while recursion already work on stack so it will be easy to ise recursion in dfs traversal
26th Apr 2021, 4:16 PM
YUGRAJ
+ 3
Coding Kitty Christmas 🎄!
27th Apr 2021, 3:22 AM
Sanjay Kamath
Sanjay Kamath - avatar
+ 3
I have never used recursion in real world project. It's inefficient and waste of memory. Your program will crash if your function is using too much recursions. You can do same thing with loops without any problem.
27th Apr 2021, 9:20 PM
🌀 Shail Murtaza شعیل مرتضیٰ
🌀 Shail Murtaza شعیل مرتضیٰ - avatar
+ 2
Yes. Absolutely. For calculating GCD (greatest common divisor).
27th Apr 2021, 2:21 PM
Mani_K_A