+ 3
Why recursion is needed in c++
5 Respuestas
+ 3
recursion has many advantages
1) it shortens code
2) it simplifies code ( in more understandable form)
3)it solves many issues of memory and time management
+ 1
Recursion is not needed in a sense that some things can only be expressed via recursion that cannot be expressed by other constructs (there's a proof in theoretical computer science that a recursive function can be transformed to an equivalent non-recursive).
Anyways, recursion is often an elegant and short way of handling "recursive" data structures as trees (or in general all data structures that model a whole-part relationship).
It's kinda hard to get into at first but it's worth it.
0
I disagree on the memory and time management part. Just because it's local variables doesn't mean the program has to allocate memory and to be honest a recursive solution is slower just due to the function call overhead and the allocation and freeing of memory.
Apart from that, a function calling itself makes it oftentimes as hard to understand as usual sequential code. I give it the elegance and shortness award, though.
0
Stefan ya i am sorry i just casually wrote memory there, actually u r right , recursion eats memory but it saves time in a sense
0
It might save programming time. As long as the performance overhead does not become too costly due to the inefficiencies of recursion, the programming time is the most costly thing.