This week we learned something about
recursion which is really efficient for solving large-scale problems. Recursion
means “defining something in terms of itself”. This approach breaks down a
complex problem into small pieces which we could apply the same algorithm to
with changing parameters until a termination condition is reached and combines
the result.
Every recursive function is a combination
of general case and base case. Base case is also called termination case. Every
recursive function must have at least one base case otherwise it will keep
calling itself. The general case is a recursive sub call which happens most of
time. We should avoid leaving out the base case when writing a recursive
function as well as circularity.
We could use both recursion and iteration
to solve problems involving repetition. Compared to iteration, recursion is
easier to read and requires fewer lines of code; however it is a little bit
slower. This is because recursion calls itself many times and stores certain
values on the stack for each function call, which takes time and stack space.
Finally, feel free to leave comments here J
No comments:
Post a Comment