From the course: Python Quick Start

Unlock this course with a free trial

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

Navigating a recursive function

Navigating a recursive function - Python Tutorial

From the course: Python Quick Start

Navigating a recursive function

A common pattern can be found in the body of many recursive functions. The body begins with one or more base cases. Base cases specify how the function should behave for the simplest inputs. In other words, base cases are very simple versions of the problem you're trying to solve. The base cases are then followed by one or more recursive cases. Recursive cases are more complex versions of the problem you're trying to solve, and they require one or more recursive calls. Recursive calls always simplify the original problem. Recursive function simplify problems in stages. For example, say you want to write a function that takes a positive integer as input and returns the factorial of that number. You could write a recursive function and it would look something like this. The if clause is the base case, 1 is the simplest input for this function. If n = 1, the factorial of n is 1. The else clause is the recursive case. Numbers that are greater than 1 are not as simple as one when it comes…

Contents