9.3 Recursive Programs

Before we look at the program used to draw the tree, we must first learn a little more about recursion. In computer science and mathematics, a recursive function is a function that calls itself.

A very simple, but erroneous, illustration of a recursive function is shown in Listing 9.1. The hello function prints the message "Hello World" and then calls itself again. If you ran this function in Python, the program would continue to call itself and print "Hello World" until Python crashed. Clearly, there is more to a successful recursive function than simply calling itself.

Listing 9.1 An erroneous recursive function

Description

A more useful way of thinking ...

Get Python Programming in Context, 4th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.