Composition of Functions

\((f \circ g)(x) = f(g(x))\) chains two functions together — you run the inner one first, then feed its output into the outer one.

By the end you'll be able to evaluate a composed function step by step, and explain why \((f \circ g)(x)\) usually isn't the same as \((g \circ f)(x)\).

Predict: for x = 3, is f(g(3)) the same as g(f(3))? Compute both with the toggle below.

Two machines are chained: g(x) = x² and f(x) = 2x + 1. Drag x to feed a value into the first machine, and watch the intermediate value flow into the second machine to produce the final value. Use the button to swap which machine runs first — that's the difference between \((f \circ g)(x)\) and \((g \circ f)(x)\).

x = 2.0 → g(2.0) = 4.0 → f(4.0) = 9.0, so (f∘g)(2.0) = 9.0

Chained machines — inner machine's output feeds the outer
intermediate value final value

Composition chains two functions so the output of one becomes the input of the next — and which one runs first changes everything.

Intuitive

Think of \(f\) and \(g\) as machines on an assembly line. In \((f \circ g)(x)\), x goes into the g-machine first, and whatever comes out gets fed straight into the f-machine — the symbol just means "feed the result of one into the other." Order matters: putting on socks then shoes is not the same as shoes then socks, and \((f \circ g)(x)\) is not generally the same as \((g \circ f)(x)\).

Formal

\((f \circ g)(x) = f(g(x))\): evaluate the inner function g at x, then evaluate the outer function f at that result. Composition is non-commutative in general — \((f \circ g)(x) \ne (g \circ f)(x)\) — because reversing the order changes which function's rule applies first. There's also a domain requirement: g(x) must be a valid input for f, or the composition is undefined at that x.

Applied

A store applies a 20% discount, then a flat $5 shipping fee — two chained rules, just like \(f(g(x))\). Charge shipping first and then discount the total, and you get a different final price, because discounting a smaller shipping-inclusive total isn't the same as adding shipping to an already-discounted price. The order you chain operations in changes the outcome, exactly like composing functions.

Worked example

Let \(f(x) = 2x+1\) and \(g(x) = x^2\). Find \((f \circ g)(3)\): first the inner function, \(g(3) = 3^2 = 9\); then the outer function, \(f(9) = 2(9)+1 = 19\). So \((f \circ g)(3) = 19\). Reversing the order gives a different function entirely: \((g \circ f)(3) = g(f(3)) = g(7) = 49\) — proof that order changes the answer. Set x = 3 on the slider above and toggle between the two orders to see both values.

Your turn

Same f and g, a different input: find \((f \circ g)(-1)\). Inner function first: \(g(-1) = (-1)^2 = 1\). Now feed that into the outer function: \(f(1) = 2(1)+1 =\) ____.

Reveal the answer

\(f(1) = 2(1)+1 = 3\), so \((f \circ g)(-1) = 3\). Set x = -1 on the slider above (with the default g-then-f order) and check the final value against this answer.

More info — why the domain of the inner function matters

Composition isn't just "plug one formula into another" — the inner function's output has to be a legal input for the outer function, or the composition breaks down at that x. For example, if \(h(x) = \sqrt{x}\) is the outer function and \(g(x) = x - 5\) is the inner one, then \((h \circ g)(x) = \sqrt{x-5}\) is only defined where \(x - 5 \ge 0\), i.e. \(x \ge 5\) — even though \(g(x)\) itself is defined everywhere. See the Math is Fun link in Dive deeper for more visual examples of chaining functions.

Check your understanding

Question 1 of 4

Let \(f(x) = 2x + 1\) and \(g(x) = x^2\). What is \((f \circ g)(4)\)?

Question 2 of 4

For the same \(f(x) = 2x + 1\) and \(g(x) = x^2\), what is \(g(f(2))\)?

Question 3 of 4

Which expression correctly represents plugging x into g first, then feeding that result into f?

Question 4 of 4

For \(f(x) = 2x + 1\) and \(g(x) = x^2\), does \((f \circ g)(5)\) equal \((g \circ f)(5)\)?

Recap

  • \((f \circ g)(x) = f(g(x))\): evaluate the inner function g first, then feed its output into the outer function f.
  • Composition is generally not commutative: \((f \circ g)(x) \ne (g \circ f)(x)\) — the order you apply the functions in changes the result.
  • To evaluate \((f \circ g)(a)\), compute \(g(a)\) first, then plug that number into f.
  • The inner function's output must be a valid input (in the domain) of the outer function, or the composition is undefined there.

Dive deeper

Sources

  • Composition of Functions