Matthew Hrehirchuk, Eric Chalmers, Charlotte Curtis, Patrick Perri
Section4.10Functions 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 squarefunction and some addition.
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.
defapothem(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
defperimeter(sides:int, length:float)->float:"""with sides more than 3 and length greater than zero"""
a_per = sides * length
return a_per
defarea(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.
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.