From the course: Functional Programming with Python

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Recursion

Recursion - Python Tutorial

From the course: Functional Programming with Python

Start my 1-month free trial

Recursion

- [Instructor] The next advanced concept we're going to look at is called recursion. Now, this is a concept that you've probably heard of before, but maybe you haven't seen it in action. Recursion is simply when a function calls itself. While doing this can very easily lead to an infinite loop if we're not careful, it can also be used to solve certain problems that aren't easy to solve otherwise using functional programming. So the best way to learn about recursion is probably just to jump right in. So let's take a look at a very simple example of using recursion. What we're going to do is create a function that behaves like a for loop without actually using a for loop. And we'll call this function count_down. So we'll say def count_down, and what it'll do is start at whatever number we pass it as an argument. We'll call that argument x, and use recursion to count down from that number to zero, printing the results…

Contents