Feeding one function into another
Composition, the notation for it, why the order matters, and its domain.
A shop takes 20 per cent off the price and then adds 10 per cent tax. A different shop adds the tax and then takes the discount off. Customers argue about which is cheaper, and the argument is settled by working out both.
Run it on a price of 100 and on a couple of others.
Change the discount to 0.8 * p - 5, a flat five off as well, and run it again. Now the two orders genuinely disagree.
With two plain percentages the answers agree, because multiplying by 0.8 and then by 1.1 is the same as multiplying by 1.1 and then by 0.8. Add a flat five pounds off and the agreement collapses, because subtracting 5 and then adding tax charges tax on a smaller amount.
Feeding the output of one function into another is called composition, and it has its own notation.
The order in the notation looks backwards until you read it as after. In the input goes into , and whatever comes out goes into . It matches the nesting in the code exactly, where the inner call happens first.
One worked example in full. With and , find and .
- For , start inside: .
- Feed that to , which doubles its input and adds 1: .
- For , start inside again: .
- Feed that to , which squares its input: .
- The two results are different rules, so the order matters.
Check that against numbers rather than trusting the algebra.
Change g to return x * x + 4 and work out both compositions on paper, then edit the two check formulas to match. If a column disagrees, the algebra is wrong.
Composition is also how a complicated rule is taken apart. Given , you can read it as the squaring function applied after the rule . So with and , . Breaking a rule into an inner and an outer part like this is worth practising, because several later techniques depend on spotting it.
The decomposition is not unique, which is worth knowing so that a different answer from a classmate does not worry you. The same can also be built with and . What matters is that composing your two rules in the stated order rebuilds exactly, so substitute and check before committing to an answer.
You have also met composition already without the notation. The transformations of the earlier lessons are compositions: is the rule that subtracts 3, then applies , then adds 2. That is why the order there was decided by the brackets, in exactly the way it is here.
The domain of a composition
A composition has two chances to fail. The input must be acceptable to the inner function, and the value that comes out must be acceptable to the outer one. Run it.
Change g to return x - 10 and run it again. The set of inputs that survive moves along with it.
Only inputs of 4 and above survive, because those are the ones whose inner value is not negative. The domain of is therefore , even though itself accepts every real number.
With and , work out and .
. . Different answers from the same two functions and the same input, which is the usual situation.