Back to courseLesson 4 of 14

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.

3inner firstg: +25outer nextf: x²25
f(g(3)) = f(5) = 25. Work inside-out: g runs first. Swap the order — g(f(3)) = 3² + 2 = 11 — and the answer changes.

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:

CompositionSteps for x = 3Result
f(g(x)) — square after adding(3 + 2)² = 5²25
g(f(x)) — add after squaring3² + 2 = 9 + 211

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))?

Next lesson