6.6. Functions can Call Other Functions

It is important to understand that each of the functions we write can be used and called from other functions we write. This is one of the most important ways that computer scientists take a large problem and break it down into a group of smaller problems. This process of breaking a problem into smaller subproblems is called functional decomposition.

Here’s a simple example of functional decomposition using two functions. The first function called square simply computes the square of a given number. The second function called sum_of_squares makes use of square to compute the sum of three numbers that have been squared.

(sumofsquares)

Even though this is a pretty simple idea, in practice this example illustrates many very important Python concepts, including local variables along with parameter passing. Note that when you step through this example, codelens bolds line 1 and line 6 as the functions are defined. The body of square is not executed until it is called from the sum_of_squares function for the first time on line 11. Also notice that when square is called there are two groups of local variables, one for square and one for sum_of_squares. As you step through you will notice that x, and y are local variables in both functions and may even have different values. This illustrates that even though they are named the same, they are in fact, very different.

Now we will look at another example that uses two functions. This example illustrates an important computer science problem solving technique called generalization. Assume we want to write a function to draw a square. The generalization step is to realize that a square is just a special kind of rectangle.

To draw a rectangle we need to be able to call a function with different arguments for width and height. Unlike the case of the square, we cannot repeat the same thing 4 times, because the four sides are not equal. However, it is the case that drawing the bottom and right sides are the same sequence as drawing the top and left sides. So we eventually come up with this rather nice code that can draw a rectangle.

def draw_rectangle(width: float, height: float) -> None:
    for i in range(0, 2, 1):
        turtle.forward(width)
        turtle.left(90.0)
        turtle.forward(height)
        turtle.left(90.0)
    return None

The program doesn’t “understand” that you’re drawing a rectangle or that the parameters represent the width and the height. Concepts like rectangle, width, and height are meaningful for humans. They are not concepts that the program or the computer understands.

Thinking like a computer scientist involves looking for patterns and relationships. In the code above, we’ve done that to some extent. We did not just draw four sides. Instead, we spotted that we could draw the rectangle as two halves and used a loop to repeat that pattern twice.

But now we might spot that a square is a special kind of rectangle. A square simply uses the same value for both the height and the width. We already have a function that draws a rectangle, so we can use that to draw our square.

def draw_square(size: float) -> None:        # a new version of drawSquare
    drawRectangle(size, size)
    return None

Here is the entire example with the necessary set up code.

There are some points worth noting here:

So far, it may not be clear why it is worth the trouble to create all of these new functions. Actually, there are a lot of reasons, but this example demonstrates two:

  1. Creating a new function gives you an opportunity to name a group of statements. Functions can simplify a program by hiding a complex computation behind a single command. The function (including its name) can capture your mental chunking, or abstraction, of the problem.

  2. Creating a new function can make a program smaller by eliminating repetitive code.

  3. Sometimes you can write functions that allow you to solve a specific problem using a more general solution.

You have attempted of activities on this page
Next Section - 6.7. Flow of Execution Summary