Lesson 7
Objects and Dictionaries
Chapter 6: Recursion
Recursive functions
Now, let us completely switch gears and talk about a different topic altogether.
Let us talk about a useful concept called recursive functions.
A recursive function is simply a function that calls itself.
def do_something(x):
print(x)
do_something(x)
do_something(5)
Of course, the code above is useless and will result in an error. Try it!
The function will forever be calling itself without stopping. Well, at least it doesn’t stop until Python hits the maximum ‘depth’ that the function can call itself. This is an infinite recursion.
So it is obvious that
- we need to do something more useful
- we need a way to stop the recursion
So let’s use recursion more wisely in the next page!