Composite Functions

Drive through a car wash and your car goes through two machines: first the washer soaks and scrubs it, then the dryer blasts it with air. The dryer never sees your original muddy car — it only ever receives whatever the washer hands it. Two machines, bolted in series, the output of one becoming the input of the next.

Shops do the same thing with prices — and here the order genuinely changes what you pay. Suppose a £50 hoodie is in a 20%-off sale, and you also have a £5-off voucher. Which gets applied first?

Same hoodie, same two rules — but chaining them in a different order costs you an extra pound. Chaining rules together, and caring about the order, is exactly what a composite function is. This page teaches you how to chain functions, how to evaluate the chain, how to write the chained rule as a single formula, and how to take a complicated function apart back into a chain.

The notation, and the golden rule

We know function notation: f(x) means "run the rule f on the input x". A composite function simply chains two rules together — you do one, then feed its output straight into the other.

We write the composite of f and g as fg(x), or more clearly as f(g(x)). (Some books write (f \circ g)(x) with a little circle — it means exactly the same thing. And f^2(x) usually means ff(x), the function f applied twice — not f(x) squared.) The golden rule is to work from the inside out:

f(g(x)) \;=\; \text{do } g \text{ first, then } f

The function nearest the x goes first. So in f(g(x)) the inner function g runs first, and whatever it produces becomes the input to f. Notice something slightly uncomfortable: even though we say "f of g", it is g that acts first — you read a composite right to left, like unwrapping a parcel from the inside.

Two machines in series

Think of it as two function machines bolted together, just like the washer and the dryer. A number drops into the first machine g; its output slides along and drops into the second machine f; what comes out the far end is f(g(x)). Press play to watch the number 3 travel through both.

Watch carefully what arrives at f. It is not the 3 we started with — f never sees the 3 at all. It receives the 6 that g produced, and works on that. This is the single most important picture on the page: the second machine works on the first machine's output, not on the original input.

Evaluating a composite: numbers first

Before we build formulas, let's just evaluate composites at a number — that's where the habit of "inside out" is formed. Take the pair we will use for the rest of the page:

f(x) = x^2 \qquad g(x) = x + 1

Find fg(3). Inside out: the inner function is g, so it goes first.

g(3) = 3 + 1 = 4 \qquad\Rightarrow\qquad fg(3) = f(4) = 4^2 = 16

Now find gf(3). This time f is the inner function — it sits nearest the 3 — so it goes first.

f(3) = 3^2 = 9 \qquad\Rightarrow\qquad gf(3) = g(9) = 9 + 1 = 10

Same two functions, same input, but fg(3) = 16 while gf(3) = 10. The order you chain the machines changes the answer — just like the sale and the voucher. Notice also that fg(3) is a number: once you feed in a specific input, out comes a specific output. Only when we leave the input as x do we get a formula.

Building the new rule

Evaluating one input at a time is fine, but the real power move is combining the two rules into one new formula that does the whole chain in a single step. The method is substitution: wherever the outer function expects its input, substitute the entire inner rule.

Warm up with the machines from the animation: f(x) = x + 1 and g(x) = 2x. The rule f says "take the input and add one" — and its input is now the whole of 2x:

f(g(x)) = f(2x) = (2x) + 1 = 2x + 1

Check it against the animation: with x = 3, g(3) = 6 and then f(6) = 7, which matches 2(3) + 1 = 7. One formula, same answer, no intermediate step.

Now the classic pair. With f(x) = x^2 and g(x) = x + 1, the rule f says "square the input" — so it squares all of x + 1, brackets and all:

fg(x) = f(x + 1) = (x + 1)^2 = x^2 + 2x + 1

The other way round, g says "add one to the input" — and its input is now the whole of x^2:

gf(x) = g(x^2) = x^2 + 1

Two genuinely different rules: x^2 + 2x + 1 versus x^2 + 1 — they differ by 2x, the cross-term the brackets create. The brackets are doing real work: forgetting them (writing x^2 + 1 for fg) is exactly the error of computing gf by accident.

See the difference

Because fg and gf are different rules, they draw different curves. Below are fg(x) = (x+1)^2 and gf(x) = x^2 + 1 on the same axes. Both are parabolas — but fg has its lowest point at x = -1 (where the inner x + 1 is zero), while gf sits at height 1 above the origin. They agree at exactly one place, x = 0, where both give 1 — everywhere else the extra 2x pushes them apart.

Order matters — always check which way round

Back to the animation's pair to drive it home. Doing f first and then g is the composite g(f(x)):

g(f(x)) = g(x + 1) = 2(x + 1) = 2x + 2

That is not the same as f(g(x)) = 2x + 1. It is the hoodie all over again: "double, then add one" and "add one, then double" disagree by exactly the doubled one. Whenever you meet a composite, your first job — before any algebra — is to read off which function is the inner one. Get that right and the rest is careful substitution.

Khan Academy introduces composing functions here: Intro to composing functions.

Chains of three (and more)

Nothing stops you bolting a third machine on the end — or a tenth. The golden rule doesn't change: start at the innermost function and work outwards, one layer at a time. Take

f(x) = x + 1 \qquad g(x) = 2x \qquad h(x) = x^2

Evaluate h(g(f(2))). Innermost is f:

f(2) = 3 \;\;\longrightarrow\;\; g(3) = 6 \;\;\longrightarrow\;\; h(6) = 36

And as a formula? Substitute outwards, one layer at a time:

g(f(x)) = 2(x + 1) \qquad\Rightarrow\qquad h(g(f(x))) = \bigl(2(x+1)\bigr)^2 = 4(x+1)^2

Sanity-check with the number: 4(2+1)^2 = 4 \times 9 = 36. ✓ Long chains look scary but are just the same move repeated — which is why the inside-out habit is worth drilling now.

Going backwards: decomposing a function

So far we've built composites. The reverse skill — looking at a complicated function and spotting the chain inside it — turns out to be even more valuable. Consider

y = (x + 3)^5

Expanding that bracket would be miserable. But describe it as a recipe: "add 3, then raise to the fifth power". That's a chain! Set g(x) = x + 3 (the inner step, done first) and f(x) = x^5 (the outer step), and

(x + 3)^5 = f(g(x))

The trick for finding the inner function: ask "what would I compute first if I plugged in a number?" For (x+3)^5 you'd add 3 first — so x + 3 is the inner function. Try it on \sqrt{x^2 + 1}: first you'd square and add one (g(x) = x^2 + 1), then square-root (f(x) = \sqrt{x}).

Why bother? Because calculus differentiates complicated functions by exactly this move: the chain rule differentiates (x+3)^5 by treating it as f(g(x)) and handling the layers one at a time. Learn to see the chain now and the chain rule will feel like an old friend rather than a new monster.

The instruction on a shampoo bottle — "lather, rinse, repeat" — is a composite function applied to your hair, and the famous joke about it (a literal-minded robot would loop forever, since "repeat" never says when to stop) is really a joke about reading a composition too literally. Recipes, IKEA instructions, morning routines: any "do this, then that, then that" is composition, and in each one the order is load-bearing. Socks then shoes is not shoes then socks.

Mathematicians take this seriously because almost every function you'll ever meet is secretly a composite. \sin(3x), e^{-x^2}, \sqrt{1 - x^2}, \frac{1}{x^2+1} — none of these is a "new" function. Each is a short assembly line of simple parts: triple it then take sine; square it, negate, then exponentiate. Computer programs run the same way — the output of one function is piped into the next, and a whole program is one enormous composition. When calculus arrives, its greatest hit — the chain rule — is nothing more than a rule for how assembly lines change speed: it handles any function you can build, because you built it from parts it understands.

See it explained