Why Use Recursion?

So far, thinking about solving problems recursively may seem more difficult than using loops. However, there are many problems that are more naturally (and more elegantly) solved using recursion. You will soon find that certain algorithms (e.g., binary search) and certain data structures (e.g., linked lists and trees) are easier to think about recursively.

Searching through a file directory is one example of a problem that is more easily solved recursively. Watch the below video to see why trying to print all of the files and folders from your computer is not simple to do using loops:

So how would we do this using recursion? We'll do this as a practice exercise at the end of the week (practice exercise 5).

Now that we know more about why recursion can be useful, let's learn the basics of how we can use recursion as a programming technique.