Being expressive with functions
Learning objectives
- Understand what it means to say that a function is dependent on a variable
- Understand how to express a multivariable function
- Understand how to express a function that is composed of another function, and why we express functions that way
Introduction
Concepts in mathematics and in code tend to align. Both are mechanisms to express an idea and model the world around it. It's time we go off on a little bit of a tangent to explore how denoting functions in math aligns with denoting functions in code. Some of these concepts may feel like review, but solidifying this foundation will provide clarity when we move on to explore other mathematical topics.
Expressing functions
Let's find a way to talk about functions in general. We describe a function as
The expression above means that there is an output that equals 3 times
Note this mathematical expression of an output varying with an input lines up nicely with expressing programmatically how a function varies with an input. In programming, we simply can use an argument to show that a function varies with an input.
def f(x):
return 3*x
f(3)
9
Evaluating functions at specific values
Let's evaluate the function
That looks pretty similar to how we would evaluate a function at a specific value in code.
f(3)
9
f(4)
12
So far it feels like a small leap to go from expressing a function in math to expressing a function in code.
$x$ in $f(x)$
Unpacking the So the
Now take a look at another function:
To determine this output, we need to know more than a specific value of
It is not just a specific input of
def f(x,y):
return 3*x + y
And to indicate that we are evaluating the function at specific values of
f(3, 4)
13
A function whose output depends on multiple variables, like
Test your knowledge
Take a look at a new function. How would we express the function:
Is that a multi-variable function? Well, while the number 4 influences the output of the function, we do not need to know anything but the value of
And in code:
def f(x):
return 3*x + 4
$f$ in $f(x)$
Unpacking the Now that we understand the significance of
You will see functions labeled differently,
Later on, we can reference the second function,
Functions depending on other functions
Now that we know how to label different functions, we can also take a look at what it means for functions to depend on other functions. In code, we see this all of the time. For example, here is a function that we have solved before.
def squared_error(actual, expected):
return (actual - expected)**2
squared_error(4, 2)
4
But we can really break this function into two:
def error(actual, expected):
return actual - expected
def squared_error(actual, expected):
return error(actual, expected)**2
squared_error(4, 2)
4
In code, composing our functions from other functions helps us break down a problem. It will be easier for us understand our code when we refer to it later on. Similarly, functional composition in mathematics can also help break down problems and assist with readability. Here is how we express it. Let's represent the function error
as
Now we can represent squared error as the following.
Take a close look at this second function. We are expressing that this second function, squared_error
function because we need to know the error
function, as well as the arguments of actual
and expected
.
Let's see one more example to make sure that we have the hang of it. Take the following function:
Now let's try to represent with functional composition. How? Here's one way:
Summary
In this section, we learned about expressing functions mathematically. We saw that when what we call a function, the letter we use, whether