Section 4.10 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, more easily solved, 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. BTW: This ’sum of squares’ process is part of a very common statistical calcuation used to determine variation and ultimately standard deviation.
Even though this is a pretty simple idea, in practice this example illustrates many very important Python concepts, including local variables, global variables, parameter passing, and how a larger function can be decomposed into smaller sub-functions.The function sum_of_squares
is composed of the square
function and some addition.
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 Steps 8, 13, and 18), 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 x
, and 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 11, 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 at this step? 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 another function: finding the area of any regular polygon.
Knowing that:
Regular polygons are 2D shapes that have more than 3 sides where all sides have the same length.
The perpendicular measurement from the midpoint of one of the sides to the center of the polygon is called the apothem.
-
The length of the apothem for a polygon with \(n\) sides of length \(length\) can be found with:
\begin{equation*}
apothem = \frac{length}{2\tan(180/n)}
\end{equation*}
-
The perimeter of the polygon can be found with:
\begin{equation*}
perimeter = length * n
\end{equation*}
Then the area of a regular polygon can be found with the following formula:
\begin{equation*}
area = \frac{perimeter * apothem}{2}
\end{equation*}
We can decompose the problem of finding the area of a regular polygon into:
Set the number of sides.
Set the length of the sides.
Determine the apothem.
Determine the perimeter.
Determine the area.
Thus the sub functions are:
def apothem (sides:int,length:float ) -> float:
"""With sides more than 3 and length greater than zero"""
from math import tan, radians # math module's tan only works in radians
an_apo = length / (2 * tan (radians(180/sides)))
return an_apo
def perimeter (sides:int, length:float) -> float:
"""with sides more than 3 and length greater than zero"""
a_per = sides * length
return a_per
def area(sides:int, length:float)->float:
"""with sides more than 3 and length greater than zero"""
apo = apothem(sides,length)
per = perimeter(sides, length)
return per * apo /2
On paper, trace what will happen, the values of the local and global variables with this function call testing a square with of 10 units long: test=area(4,10)
. Try some other polygons of your own to test the algorithm. In your own words, express how the algorithm to find the area of a regular polygon has been functionally decomposed.
Check your Understanding
Checkpoint 4.10.1.
1. Write two functions, one called addit
and one called mult
.
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.
You have attempted
of
activities on this page.