Section 19.1 Functions and Composition
So far we have focused on writing procedures - problem-solving recipes that do part of a job and return no answer. Now we will shift our attention to functions - problem-solving recipes that are designed to calculate some value and return it to the code that calls the function so we can do more work with the result.
In the sample below, we can see the importance of returning results from functions so that we can string together several steps. Line 3 calls the input
function to get a string like "4"
from the user and store it as sideAText
. Then on line 5, we use the float
function to turn that string into a numeric value 4.0
and store that in sideA
. We then use math.pow
to raise that value to the second power and store the result as aSquared
. aSquared
gets used as part of the input to the math.sqrt
function which produces our final answer sideC
which gets passed to the print
function.
The fact that every function (other than print
) returns a value is what allows us to chain them together by using the result of one function as the input to the next function. We take the returned value (the output of the function) and store it into a variable so we can pass it on to the next as input.
Note that just like with procedures, some functions like input
are standalone and are called on their own. Other functions are part of a library or object and must be called with dot notation like math.pow
.
Subsection 19.1.1 Function Composition
We can more directly specify to use the result of calling one function as the input to another function via functional composition. That is where we put one function call inside of another - like how this version of the same program does float(input("Enter the length of side A"))
.
Like when doing math, we always do work inside of parentheses before worrying about the work outside of them. In this case, that means we start with the "Enter length of side A"
. It is just a string, so there is nothing special to do with it other than use it as the argument to input()
. Then the input
function runs and does its job to get input from the user. It returns a string with whatever the user typed. Recall that the mental model you should use for function calls is that they are replaced by the value they return. So input("Enter length of side A")
ends up just becoming something like "4"
. That value is what gets used as the argument for float()
.
When sqrt(pow(sideA, 2) + pow(sideB, 2))
is evaluated, we have to start with the inner functions and get answers for pow(sideA, 2)
and pow(sideA, 2)
. Then we can add those answers to get one number. That one number ends up being the argument for sqrt
which takes the square root of it and returns the answer.
Checkpoint 19.1.2.
int()
takes a number and returns just the whole number part (without rounding). int(3.9)
would return 3. abs()
takes one number and returns it as a positive value.
What do you think print(int(abs(-16.789) + 1)), prints?
15
Make sure to do the abs function first. Then add one.
16
Make sure to do the abs function first. Then add one.
17
Correct.
18
The int function will not round the value up. It just removes the decimal part.
Checkpoint 19.1.3.
It would not make sense to put a procedure call like alex.forward(50)
inside a function call to make something like int(alex.forward(50))
.
Why does it not make sense to put a procedure call inside of a function?
Procedures always happen after functions.
No, Python always evaluates the innermost function or procedure first if there are composed call.
The procedure does not return anything. There will not be a value for the function to work with.
Correct.
You can’t mix functions and procedures.
No. You can mix functions and procedures. We have seen the int function called inside of the print procedure.
You have attempted
of
activities on this page.