From the course: Programming Foundations: Algorithms

Unlock the full course today

Join today to access over 24,800 courses taught by industry experts.

Understanding recursion

Understanding recursion

- [Instructor] There's one more programming technique that we're going to take a look at before we start working directly with some common algorithms, and that's a technique known as recursion. In its simplest form, recursion is when a function calls itself from within its own code. Now, that might seem a little strange at first. I mean, after all, why would you want to write a function that calls itself? But it turns out that this kind of programming pattern appears in multiple scenarios in the real world and will examine some of them in this chapter. Suppose for example, you had a program that needed to find a particular file that's nestled somewhere within a set of directories, and your program doesn't know how many directories there are, and of course these directories can contain other directories and so on. So what algorithm would you use to search for the file? Well, one possibility might look like this. You start…

Contents