Composition of functions
One function cannot do very much on its own. That is why functions can be combined, or composed.
Motivation
Let’s take two functions. The first works out how many kilometres a car travels on a given amount of petrol. Say our car travels 10 kilometres on one litre of petrol. We call this function f and define it as f(x) = 10x, where the parameter x is the number of litres of petrol we have. So if we have six litres in the tank, the car can still travel f(6) = 10 · 6 = 60 kilometres.
The second function can work out how many litres of petrol we buy for a given amount of money. Say a litre of petrol costs £2. Then the function, which we call g, would look like this: g(x) = x / 2, where x is how many pounds we want to spend on petrol. If we have £10, we would buy g(10) = 10 / 2 = 5 litres of petrol.
Now we could ask: how many kilometres can we travel if we buy £50 of petrol?
We already have functions that each work out one part – the number of kilometres travelled depending on the litres, and the number of litres bought depending on the money. Now we need to combine the two functions.
How to solve it
We could solve it by first working out how many litres of petrol we would buy and then how many kilometres we could travel. Let’s try it. For £50 we would buy g(50) = 50 / 2 = 25 litres of petrol. And on 25 litres of petrol we could travel f(25) = 10 · 25 = 250 kilometres.
But we can also solve it another way, by composing functions. We can define a new function h, which takes an amount of money as its input and returns the number of kilometres travelled. We define the function h using the functions f and g like this:
The function h defined in this way does exactly what we calculated in the first paragraph. It tells us that when we are given a value x, i.e. an amount of money, we first work out the value g(x), which is the number of litres we buy for it. We then put this resulting value g(x), in our case g(50) = 25, into the function f, so we also work out f(25) = 250.
We can also get a direct formula for the function h. We take the function f and replace every occurrence of the parameter x with the function g. The function f looks like this: f(x) = 10x, and we substitute the function x / 2 for the parameter x. This gives us the function h:
Let’s simplify it:
We have now composed the functions f and g into a new function h, which works out how many kilometres we can travel on x pounds’ worth of petrol. We can check it by putting in our good old £50:
The result agrees with our earlier calculation.
Composing more complicated functions
Let’s take functions f and g defined like this:
Let’s now compose them like this: f(g(x)). We take the function f and, in place of every occurrence of the parameter x, put in the function g, preferably in brackets:
If we composed the functions the other way round, we would get:
A small circle is used as the symbol for composing functions: f ∘ g. The trouble is that this notation sometimes means composing in the direction f(g(x)) and sometimes the opposite, g(f(x)). The notation is a bit ambiguous here, so always check what is meant.
Chapters: What is a function, Functions of several arguments, Composition of functions, The graph of a function