- 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 to express functions that way
As we have seen, our concepts in mathematics and in code line up pretty nicely. It's time we go off on a little bit of a tangent to explore how the way we denote functions in math lines up to how we denote functions in code. Through this, we will also learn different ways of denoting functions with math. Some of these concepts may feel like review, but making sure we have a handle on them will give us clarity when we move on to explore other topics in mathematics.
So let's begin a way to talk about functions in general. We describe a function as
The above just means that there is an output that equals 3 times
Note that our mathematical expression of an output varying with an input lines up nicely to how a function varies with input. To show that a function varies with an input, we just use an argument.
def f(x):
return 3*x
Now 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 the
Now take a look at another function:
Now to determine this output, we need to know more than a specific value of
def f(x,y):
return 3*x + y
And to evaluate indicate that we are evaluating the function at specific values of
f(3, 4)
13
A function whose output depends on multiple variables, like
Now how would we express the function:
Is that a multi-variable function? 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
So now that we understand the significance of
You will see functions labeled differently,
And now in the future, we can just reference the second function as
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 readability as well as break problems down. 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 $g(x, y) = x - y $ where
Now we can represent squared error as the following.
So now we are expressing that this second function, squared_error
function will return, we need to know about our error
function, as well as the arguments of actual
and expected
.
Let's do one more to make sure we have the hang of it. Take the following function:
How could we represent this as multiple functions? Here's one way:
In this section, we learned about expressing functions mathematically. We saw that when what we call a function, whether