Function composition connects two functions in sequence. In \(f(g(x))\), the input goes into \(g\) first, and the output of \(g\) then becomes the input of \(f\). The inside function acts first, and the outside function acts last.

Follow the pipeline

The upper band is the pipeline: x enters g, and g's output enters f. The graph below it draws the composite f(g(x)) in ink, with g dashed and f dotted for reference. Where the composite line breaks, the first machine has produced a value the second cannot accept. Swap the order and the composite changes shape. Composition is not commutative, and the graph shows it immediately.

The visualization shows the intermediate value between the two functions, and it also graphs the composite. If the first function produces an output that the second function cannot accept, the composite is undefined there. Swapping the order generally changes the result.

Order matters

Let \(g(x)=2x+1\) and \(f(x)=x^2\). At \(x=3\), the inside function produces \(g(3)=7\), so \(f(g(3))=f(7)=49\). Reverse the order, and first \(f(3)=9\), then \(g(f(3))=g(9)=19\). Thus \(f\circ g\) and \(g\circ f\) are different functions, and composition is not commutative.

Reading a composite from the outside

Suppose \(h(x)=\sqrt{3x+1}\). To decompose it, ask what operation happens last. The final operation is a square root, so a natural choice is \(f(u)=\sqrt{u}\) and \(g(x)=3x+1\), and then \(h(x)=f(g(x))\). Giving the inside expression a temporary name often makes the structure easier to see, so write \(u=3x+1\) and then \(h=\sqrt{u}\).

The decomposition is not always unique. For \((x+2)^6\), one valid decomposition is a sixth power applied to \(x+2\), and another is a cube applied to \((x+2)^2\). A useful decomposition is usually the one that makes the relevant operation easiest to analyze.

Domains pass through both functions

Suppose \(g(x)=2x+1\) and \(f(x)=\sqrt{x}\), so that \(f(g(x))=\sqrt{2x+1}\). For the composite to exist, \(x\) must be in the domain of \(g\), and \(g(x)\) must lie in the domain of \(f\). Here that means \(2x+1\ge0\), so \(x\ge-\tfrac12\). In general,

\[x\in\operatorname{dom}(f\circ g)\quad\text{exactly when}\quad x\in\operatorname{dom}(g)\ \text{and}\ g(x)\in\operatorname{dom}(f)\]

Simplifying an expression can sometimes hide an original restriction, so it is worth determining the domain from the composition itself.

Composition appears everywhere later

Graph transformations are compositions. Exponential and logarithmic models are compositions as well, and inverse functions undo compositions in reverse order. In calculus, the chain rule differentiates compositions by identifying the same inner and outer layers. The notation becomes more elaborate later, but the underlying reading skill is the same.

A useful self-test is to compare \((\sqrt{x})^2\) with \(\sqrt{x^2}\). The first is defined only for \(x\ge0\) and simplifies to \(x\) on that domain, while the second is defined for every real \(x\) and equals \(\vert x\vert\). The operations look similar, but their order changes both the output and the domain.