+ 2
Why programmers use recursion method?
4 Respostas
+ 8
sometimes recursion is the only best method, to solve any problem. In most of the DATA STRUCTURES recursion is the only best method to achieve the goal.
For example, I hope you heard about the problem "TOWER OF HANOI". If you solve this problem with recursive approach(using recursion), it will be less than 10 lines. But with iterative approach (normal function) it will take more than 20 lines. There are lots of examples.
like : tree, heap, graph....
I hope you will understand it.
+ 8
A Recrusive method calls its self until conditions are met
+ 2
Recursion is just one of aproaches to some classes of tasks. Short code doesn't mean it's good code. Your code is not to make 'effect' on your mates but to solve the task with less resources (cpu time, memory,data transfers...). To use recursion your task has to be defined in recursive manner and it's not possible every time. Each function call costs some memory in CPU stack data structure so it's not free.