18 AnswersNew Answer
It depends on what you are programming. In fourty years of programming I can think of only a handful of times where I have used it outside of a "write a recursive function that..." academic problem. Recursive decent parsers mostly. I can't recall ever having used it in an embedded environment, which is where I do most of my coding. Recursive functions are like that special tool hanging on the garage wall that you rarely use. However, when it's needed you'll be glad you have it.
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 :)
I don't do competitive programming. I have to solve real world problems on time and within budget.
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. 🙏
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.
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.
Sometime it is good to use recursion cause it make problem easier like in dfs graph traversal
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.
Yes of course...like Fibonacci series....or I believe fractals like spiral galaxies or say, sunflower florets...
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
Yep the a-holes at interviews love recurssion questions even though they know they've never used it in a project themselves in the past 10 years.
Martin Taylor not to mention, binary trees, when was your last time when you used a binary tree?
Anvar Muradaliyev Men yaxshiman. Rahmat. Sizchi? Iltimos, ingliz tilida yozing, chunki men o'zbek tilidan ingliz tiliga google-ga tarjima qilishim kerak. 😁
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
Coding Kitty Christmas 🎄!
Yes. Absolutely. For calculating GCD (greatest common divisor).
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.