Because functions are first-class objects (4.12.1.2), a functional language can treat g∘f as a new function in its own right: it can be given a name, passed to another function or returned as a result.
Chains of composition
You can compose more than two functions: h∘g∘f means apply f first, then g, then h.
Worked example
Task: let f(x)=2x and g(x)=x−5 on the integers. Find (g∘f)(x) and (f∘g)(x), and evaluate each at x=4.
(g∘f)(x)=g(f(x))=g(2x)=2x−5. At x=4: f(4)=8, then g(8)=3. The answer is 3.
(f∘g)(x)=f(g(x))=f(x−5)=2(x−5)=2x−10. At x=4: g(4)=−1, then f(−1)=−2. The answer is -2.
The two compositions are different, which shows that the order matters.
Exam tip: in g∘f, f is applied first. Don't read it left to right! Write the intermediate step, such as g(f(x)), and check that the co-domain of f matches the domain of g.
Check yourself
In g∘f, which function is applied first?
f is applied first, and then g is applied to the result.
If f:A→B and g:B→C, what are the domain and co-domain of g∘f?
The domain is A and the co-domain is C.
Let f(x)=x+1 and g(x)=3x. What is (g∘f)(2)?
f(2)=3, then g(3)=9.
Is composition of functions commutative?
No. In general g∘f and f∘g are different functions.