We are halfway through the semester now. So far, the main topics
we learned about are inheritance, Abstract Data Types(ADTs), linked lists and
recursions. The midterm covered inheritance and ADTs so I know most people
already know what they are. Therefore, in this slog, I will be talking about
the newest topic we learned, which is recursion.
When I first heard
about the word recursion, it reminded me of induction. This is because, when I
was learning about weak induction and strong induction in Calculus II, the
professor would often say that to check if its a strong induction, you need to
see if the sequence is recursive. At that time, I didn't really fully
understand what that meant. However now, I finally understand what recursion
means. Recursion is a function that calls itself until it is forced to stop.
What this means is that it will have an exit code which will make it stop,
however until then it will call itself. Usually, a recursion function will have
two or more conditions. One condition will return a value directly, if the
condition is met, without using any recursion like an exit code. The other
condition/s will be the main part of recursion where it will break the input
into small parts and solve it one at a time. The function will call itself over
and over again until the small parts are solved. In the end it will combine all
the answers and return it.
Also recursions
are very similar to loops. Sometimes it's more time efficient and easier to
read than loops. Therefore, it is a great substitute for loops in certain
situations.
No comments:
Post a Comment