Sunday, 1 March 2015

SLOG Week 7: Recursion

Recursion:
Recursion is useful for when someone needs a function that solves a problem by repeating a particular process over and over again for smaller and smaller inputs. The main characteristic of a recursive function is that the function calls itself.

Recursion is great for many reasons. It may be easier for people to understand as recursive functions and are usually shorter and they can make certain functions MUCH easier to code. However, recursion tends to take up more memory as the computer has to keep track of all the function calls and variables.

From what we've learned in class, a good way to start solve a problem that requires a recursive function is to write an if statement for what happens during the simplest case (base case) of a problem, a case where recursion is not needed. Then, the rest of the cases have to be covered afterwards. A good way to do this is to try to think of what needs to be returned when an input that is very simple, but not as simple as the base case, is given.

No comments:

Post a Comment