Week 7: Recursion
Recently, I have found out for myself new thing - recursion. Now, I don't even know how I would imagine programming language without recursion. Recursion is really makes everything easy compare to "you-do-all".
To begin with, to implement a recursive function, you need to remember three simple steps that are base case, recursive step and work.
The base case step is very less or more crucial compare to two other steps because you need to define, when your program should stop going further and extract the answer right away. For that, you first all of all, you really need to understand what program should do. Then, the rest will be easy.
Recursive step as it's written means you write a real recursive line that will call the program itself and recursively solve it. For this step, you need decide which part of your problem you want to apply recursion, so it will make easy your job.
The last step is work. In this step, you simply get results from recursive step and combine them to return or do something else.
So to clarify my understanding about recursion, I want to explain benefits of using it. I found recursion very efficient way of solving for any kind of programming problems, especially, for running time efficiencies. We know that any kind of loops may take exponential time or more when recursive function could perform the same job in a linear time or less. So from programming perspectives, it is very handy to use recursion. The second advantage of recursion, it does the job for you, so you do not have to iterate through all of your loops, just simply extract the answer you need. It may look like, if we imagine recursion as tons of minions who do each little work and give you one-done job at the end, when iterative function as Spiderman fighting with all monsters alone.


Actually, loops are basically always faster and more efficient than recursion.
ReplyDeleteI had been convincing myself that recursion is more efficient than loops. Thank You!
ReplyDelete