Why programmers use recursion method? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Why programmers use recursion method?

12th Mar 2018, 8:52 AM
S. M. Golam Sarwar Apu
S. M. Golam Sarwar Apu - avatar
3 Answers
+ 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.
12th Mar 2018, 10:44 AM
Ajay Agrawal
Ajay Agrawal - avatar
+ 8
A Recrusive method calls its self until conditions are met
12th Mar 2018, 9:59 AM
D_Stark
D_Stark - avatar
+ 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.
17th Mar 2018, 6:48 AM
Andrey Smirnov
Andrey Smirnov - avatar