Composing functions
What you'll learn
Feed one function's output into another, f(g(x)), and see why the order changes the answer.
You can chain functions: run one, then feed its output into another. This is composition, and it's how complicated functions are really built out of simple ones.
Output of one becomes input of the next
Write it f(g(x)) — read "f of g of x". The inner function g runs first; its output is handed to the outer function f.
With g(x) = x + 2 and f(x) = x²:
- g(3) = 5 — inner runs first
- f(5) = 25 — outer takes that result
So f(g(3)) = 25. There's also a shorthand for the combined machine: (f ∘ g)(x), the "composition of f and g".
Order matters
Composition is not like addition — swapping the order usually changes the answer:
| Composition | Steps for x = 3 | Result |
|---|---|---|
| f(g(x)) — square after adding | (3 + 2)² = 5² | 25 |
| g(f(x)) — add after squaring | 3² + 2 = 9 + 2 | 11 |
Same two functions, same input, different answers. Always work from the inside out.
Watch the domain
The inner output has to be a legal input for the outer function. If f(x) = √x and g(x) = x − 5, then f(g(x)) = √(x − 5) only works when x − 5 ≥ 0 — that is, x ≥ 5. Composition can shrink the domain.
Why this matters
Calculus leans on composition constantly — the chain rule is the rule for differentiating f(g(x)). And in code, data pipelines are composition: each step transforms the output of the one before it. Reading inside-out is the skill.
Check your understanding
Question 1 of 2
With f(x) = x² and g(x) = x + 1, what is f(g(2))?