Skip to content
Glacius
FoundationsConcept reference

Function composition

A composition applies an inner function to an input and then applies an outer function to the result.

On this page 7 sections
  1. Overview
  2. Let g(x)=x+2 and f(x)=2x
  3. Order matters
  4. Check your understanding
  5. Key takeaway
  6. Sources & further reading
  7. Concept connections

01Let g(x)=x+2 and f(x)=2x#

Let g(x)=x+2g(x)=x+2 and f(x)=2xf(x)=2x. In f(g(3))f(g(3)), the inner rule gg receives 3 first. Its output then becomes the input to ff.

To evaluate f(g(3)), rule g adds 2 first, producing 5; rule f then doubles 5 to 10.To evaluate f(g(3)), rule g adds 2 first, producing 5; rule f then doubles 5 to 10.
Figure 1To evaluate f(g(3)), rule g adds 2 first, producing 5; rule f then doubles 5 to 10.
Link to this figure ↗Download SVGDownload PNG

Let g(x)=x+2g(x)=x+2 and f(x)=2xf(x)=2x. First, g(3)=5g(3)=5. Then f(5)=10f(5)=10. The notation (fg)(3)(f\circ g)(3) means the same thing as f(g(3))f(g(3)).

Check your reasoning

If g(x)=x+2g(x)=x+2 and f(x)=2xf(x)=2x, what is f(g(4))f(g(4))?

Show answer and explanation
12

First g(4)=6g(4)=6. Then f(6)=12f(6)=12.

02Order matters#

Order matters. Let g(x)=x+2g(x)=x+2 and f(x)=2xf(x)=2x. Then g(f(3))g(f(3)) doubles first, then adds 2: g(6)=8g(6)=8. In the reverse order, f(g(3))=10f(g(3))=10.

Check your reasoning

In g(f(5))g(f(5)), which rule receives 5 first?

  1. Agg
  2. Bff
  3. CBoth at once
Show answer and explanation
ff

Compute the inner f(5)f(5) first.

03Check your understanding#

Check your reasoning

A data pipeline first subtracts 1 from a reading, then triples that result. What final output comes from input 4?

Show answer and explanation
9

Stage one gives 41=34-1=3. Stage two gives 3×3=93\times3=9.

Key takeaway

Work from the inside outward: the inner output becomes the outer input.

  • Evaluate a composition in the correct order

Sources & further reading

  1. [1]

Reference this concept

Link to this page, a section, or an individual figure.

Glacius. “Function composition.” Math behind ML. /learn/f-compose