Before you keep reading...
Runestone Academy can only continue if we get support from individuals like you. As a student you are well aware of the high cost of textbooks. Our mission is to provide great books to you for free, but we ask that you consider a $10 donation, more if you can or less if $10 is a burden.
Before you keep reading...
Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.
12.11. Functions can call other functions (Composition)¶
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 programmers 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
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.
Even though this is a pretty simple idea, in practice this example illustrates many very important Python concepts,
including local and global variables along with parameter passing. Note that the body of
square is not executed
until it is called from inside the
sum_of_squares function for the first time on line 6.
Also notice that when
square is called (at Step 8, for example), there are two groups of local variables, one for
square and one for
sum_of_squares. Each group of local variables is called a stack frame. The variables
y are local variables in both functions. These are completely different variables, even though they
have the same name. Each function invocation creates a new frame, and variables are looked up in that frame. Notice
that at step 9, y has the value 25 is one frame and 2 in the other.
What happens when you to refer to variable y on line 3? Python looks up the value of y in the stack frame for the
square function. If it didn’t find it there, it would go look in the global frame.
Let’s use composition to build up a little more useful function. Recall from the dictionaries chapter that we had a two-step process for finding the letter that appears most frequently in a text string:
Accumulate a dictionary with letters as keys and counts as values. See example.
Find the best key from that dictionary. See example.
We can make functions for each of those and then compose them into a single function that finds the most common letter.
Check your Understanding
1. Write two functions, one called
addit and one called
addit takes one number as an input and adds 5.
mult takes one number as an input, and multiplies that input by whatever is returned by
addit, and then returns the result.