+ 25
Is recursive functions frequently uses?
18 Antworten
+ 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 :)
+ 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.
🙏
+ 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.
+ 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.
+ 5
Sometime it is good to use recursion cause it make problem easier like in dfs graph traversal
+ 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.
+ 5
Yes of course...like Fibonacci series....or I believe fractals like spiral galaxies or say, sunflower florets...
+ 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
+ 5
Martin Taylor not to mention, binary trees, when was your last time when you used a binary tree?
+ 4
Salom qalaysz
+ 4
Anvar Muradaliyev Men yaxshiman. Rahmat. Sizchi? Iltimos, ingliz tilida yozing, chunki men o'zbek tilidan ingliz tiliga google-ga tarjima qilishim kerak. 😁
+ 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
+ 3
Coding Kitty Christmas 🎄!
+ 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.
+ 2
Yes. Absolutely. For calculating GCD (greatest common divisor).