Composition of Functions

Learning goals

  • Compute g∘fg \circ f, reading it right to left with ff acting first
  • Decide whether g∘fg \circ f equals f∘gf \circ g for a given pair
  • Find the domain of g∘fg \circ f from the range of ff
  • Decompose a function as g∘fg \circ f, knowing the split is a choice
  • Write f2f^2 for f∘ff \circ f, never for the squared output

Reading a composition right to left

Give the chaining its symbol. The composition of ff and gg, written g∘fg \circ f, is the function

(g∘f)(x)=g(f(x)).(g \circ f)(x) = g(f(x)).

Check it against the numbers above: with f(x)=x+1f(x) = x + 1 and g(x)=x2g(x) = x^2,

(g∘f)(2)=g(f(2))=g(3)=9,(g \circ f)(2) = g(f(2)) = g(3) = 9,

exactly the 2→3→92 \to 3 \to 9 chain from the opening. You feed xx to ff, and then feed the result f(x)f(x) to gg; the circle is the operation sign, exactly as ++ is the sign for addition.

The one thing to fix in your reading, because it causes more confusion than anything else in this lesson, is the order. In g∘fg \circ f the function on the right, ff, acts first, and the function on the left, gg, acts second. The notation runs right to left, opposite to the way you read a sentence. The reason is built into the definition: g(f(x))g(f(x)) has ff in the inner parentheses, and the inner operation always happens before the outer one. So g∘fg \circ f means “do ff, then gg,” never “do gg, then ff.”

The composite g of f as two machines in a rowInput x flows into box f, out as f of x, into box g, out as g of f of x, with a brace under the two boxes labeled g composed with f.xff(x)gg(f(x))g ∘ f
The composite g of f as a two-machine pipeline. The input x enters f and comes out as f(x); that value enters g and comes out as g(f(x)). The machines run left to right, but we write g of f because g acts on the output of f, so the name reads right to left and f goes first.

Worked example 1 Both orders of the same two functions

Let f(x)=x+1f(x) = x + 1 and g(x)=x2g(x) = x^2. Compute g∘fg \circ f and f∘gf \circ g, keeping track of which function acts first.

For g∘fg \circ f, the inner function is ff, so substitute f(x)=x+1f(x) = x + 1 into gg:

(g∘f)(x)=g(f(x))=(x+1)2=x2+2x+1.(g \circ f)(x) = g(f(x)) = (x + 1)^2 = x^2 + 2x + 1.

For f∘gf \circ g, the inner function is now gg, so substitute g(x)=x2g(x) = x^2 into ff:

(f∘g)(x)=f(g(x))=x2+1.(f \circ g)(x) = f(g(x)) = x^2 + 1.

The two results, x2+2x+1x^2 + 2x + 1 and x2+1x^2 + 1, are different functions. Swapping the order changed the answer, which is the first sign that composition, unlike addition, does not always let you reverse the two inputs.

Check your understanding

For f(x)=2xf(x) = 2x and g(x)=x−3g(x) = x - 3, which expression is (g∘f)(x)(g \circ f)(x)?

Answer choices

Order matters, but not always

The one law that addition has and composition often lacks is commutativity: for numbers, a+b=b+aa + b = b + a always. For functions, g∘fg \circ f and f∘gf \circ g are usually different, and Worked Example 1 already showed why: running the same two machines in the opposite order gave x2+2x+1x^2 + 2x + 1 instead of x2+1x^2 + 1. These differ by 2x2x, which is zero only at x=0x = 0.

But “usually different” is not “never equal,” and the difference is worth stating exactly, because overshooting it is the classic error. Some pairs genuinely commute. Take the two translations f(x)=x+1f(x) = x + 1 and g(x)=x+2g(x) = x + 2. Then

(f∘g)(x)=(x+2)+1=x+3=(g∘f)(x),(f \circ g)(x) = (x + 2) + 1 = x + 3 = (g \circ f)(x),

and the same works for any two translations f(x)=x+af(x) = x + a and g(x)=x+bg(x) = x + b, since both orders give x+a+bx + a + b and addition of the shifts does not care about order. So the correct statement names both cases: composition is not always commutative, but it is not never commutative either; commuting is the exception, not the rule.

Composition has two more things in common with addition, each worth a name. It is associative: for any three functions, grouping does not matter, (h∘g)∘f=h∘(g∘f)(h \circ g) \circ f = h \circ (g \circ f), so you can write h∘g∘fh \circ g \circ f with no parentheses.

And it has an identity: a function id⁡(x)=x\operatorname{id}(x) = x that changes nothing when composed with ff, the way 00 changes nothing under addition. Which set the identity is defined on matters, so when we need to be specific we write id⁡S\operatorname{id}_S for the identity on the set SS. Throughout this lesson, when a function’s codomain is not stated separately, take it to be that function’s range, the values it actually produces. Every domain and identity claim below relies on that convention.

Check your understanding

For which pair do the two compositions agree, (f∘g)(x)=(g∘f)(x)(f \circ g)(x) = (g \circ f)(x) for every xx?

Answer choices

The domain of a composition

Here is the idea the substitution procedure hides, and it is the most important thing in the lesson. When you build (g∘f)(x)=g(f(x))(g \circ f)(x) = g(f(x)), two things must be true for the answer to exist: ff must accept xx, and gg must accept the number f(x)f(x) that comes out.

The domain of g∘fg \circ f#

By definition (g∘f)(x)=g(f(x))(g \circ f)(x) = g(f(x)), so this value exists exactly when both steps inside it are legal. First f(x)f(x) must be defined, which requires x∈dom⁡fx \in \operatorname{dom} f. Then gg is applied to the number f(x)f(x), which is legal exactly when f(x)∈dom⁡gf(x) \in \operatorname{dom} g. So

dom⁡(g∘f)={ x∈dom⁡f:f(x)∈dom⁡g }.\operatorname{dom}(g \circ f) = \{\, x \in \operatorname{dom} f : f(x) \in \operatorname{dom} g \,\}.

This is not dom⁡f∩dom⁡g\operatorname{dom} f \cap \operatorname{dom} g. The second condition constrains f(x)f(x), an output of ff, so testing it needs the range of ff, not its domain. A value of xx can lie in both dom⁡f\operatorname{dom} f and dom⁡g\operatorname{dom} g yet be excluded, because f(x)f(x) escapes dom⁡g\operatorname{dom} g. Or it can lie outside dom⁡g\operatorname{dom} g and still be fine, as long as x∈dom⁡fx \in \operatorname{dom} f and f(x)∈dom⁡gf(x) \in \operatorname{dom} g.

Worked example 2 A square root inside a reciprocal

Let f(x)=xf(x) = \sqrt{x} and g(x)=1x−2g(x) = \dfrac{1}{x - 2}. Find a formula for g∘fg \circ f and its domain.

The formula is a direct substitution:

(g∘f)(x)=g(x)=1x−2.(g \circ f)(x) = g(\sqrt{x}) = \frac{1}{\sqrt{x} - 2}.

Now the domain. First ff must accept xx, and x\sqrt{x} needs x≥0x \ge 0. Second gg must accept the output x\sqrt{x}, and gg rejects only the input 22, so we need x≠2\sqrt{x} \ne 2. Since x=2\sqrt{x} = 2 exactly when x=4x = 4,

dom⁡(g∘f)={ x≥0 and x≠4 }.\operatorname{dom}(g \circ f) = \{\, x \ge 0 \text{ and } x \ne 4 \,\}.

Compare that with the wrong shortcut. The intersection dom⁡f∩dom⁡g\operatorname{dom} f \cap \operatorname{dom} g would keep x=4x = 4 and throw out x=2x = 2, the value gg forbids. The truth is the reverse: x=2x = 2 is fine, because 2≠2\sqrt{2} \ne 2, while x=4x = 4 is excluded, because 4=2\sqrt{4} = 2 is the one input gg cannot take. The excluded input is found through the range of ff, not by intersecting the two domains.

The domain of g of f as the inputs of f whose image lands in the domain of gTop line the domain of f with 0, 4, and 9 all included as closed points; bottom line the values g accepts with an open circle at 2; arrows send 0 to 0, 9 to 3, and 4 to the forbidden value 2, so 4 is excluded from the composition domain, not from the domain of f.dom f: the inputs x ≥ 0049023values g accepts: everything except 2f4 maps to 2
The domain of g of f is the part of dom f whose image lands where g is defined. Here f(x) = sqrt(x) sends the inputs x greater than or equal to 0 (top, and 4 is one of them) into the values g accepts (bottom, everything except 2). The inputs 0 and 9 map to 0 and 3 and survive; the input 4 is in dom f but maps to the barred value 2 (dashed), so 4 is removed from dom(g of f), not from dom f. Hence dom(g of f) is x greater than or equal to 0 with x not equal to 4.

Check your understanding

Let f(x)=xf(x) = \sqrt{x} and g(x)=1x−3g(x) = \dfrac{1}{x - 3}. What is the domain of g∘fg \circ f?

Answer choices

Worked example 3 Same formula, smaller domain

Let f(x)=xf(x) = \sqrt{x} and g(x)=x2g(x) = x^2. Find g∘fg \circ f, and check its domain before deciding whether it is an identity function.

Substituting gives a formula that simplifies all the way down:

(g∘f)(x)=g(x)=(x)2=x.(g \circ f)(x) = g(\sqrt{x}) = (\sqrt{x})^2 = x.

The formula alone looks like the identity. Check the domain before trusting that. First ff needs x≥0x \ge 0; then gg accepts every real number that comes out, so nothing more is excluded. So

dom⁡(g∘f)={ x≥0 }.\operatorname{dom}(g \circ f) = \{\, x \ge 0 \,\}.

So g∘fg \circ f is the rule x↦xx \mapsto x with domain x≥0x \ge 0: that is id⁡[0,∞)\operatorname{id}_{[0,\infty)}, the identity on [0,∞)[0, \infty), but not id⁡R\operatorname{id}_{\mathbb{R}}, the identity on all of R\mathbb{R}. The two share a formula but not a domain, so by the equality test they are different functions. The simplified xx threw away the record of where x\sqrt{\phantom{x}} refused to run; only the domain remembers it.

The other order behaves differently still. Here (f∘g)(x)=x2=∣x∣(f \circ g)(x) = \sqrt{x^2} = \lvert x \rvert, defined for every real xx, so dom⁡(f∘g)\operatorname{dom}(f \circ g) is all of R\mathbb{R} while dom⁡(g∘f)\operatorname{dom}(g \circ f) is only x≥0x \ge 0, a reminder that dom⁡(f∘g)\operatorname{dom}(f \circ g) and dom⁡(g∘f)\operatorname{dom}(g \circ f) are in general different sets.

Decomposing a function, and why the answer is not unique

Composition also runs backward as a question. Given a function hh, can you write it as g∘fg \circ f for simpler pieces ff and gg? This is decomposition, and it lets you see a complicated rule as a short pipeline of easy steps.

Take h(x)=3x+1h(x) = \sqrt{3x + 1}. The natural split peels off the outermost operation, the square root, and calls the inside a separate step: let f(x)=3x+1f(x) = 3x + 1 and g(x)=xg(x) = \sqrt{x}. Then

(g∘f)(x)=g(3x+1)=3x+1=h(x).(g \circ f)(x) = g(3x + 1) = \sqrt{3x + 1} = h(x).

But this is not the only way. A different split absorbs the 33 elsewhere: f(x)=3xf(x) = 3x and g(x)=x+1g(x) = \sqrt{x + 1} also gives g(f(x))=3x+1=h(x)g(f(x)) = \sqrt{3x + 1} = h(x). Both are correct, since each composes to hh. So “decompose hh” has more than one right answer. Among the valid splits there is usually a most natural one, peel off the outermost operation, but no split is the unique correct answer.

Worked example 4 Decomposing a cube

Write h(x)=(2x−5)3h(x) = (2x - 5)^3 as a composition g∘fg \circ f.

Peel off the outermost operation, the cube. Let f(x)=2x−5f(x) = 2x - 5 be the inside and g(x)=x3g(x) = x^3 be the cube:

(g∘f)(x)=g(2x−5)=(2x−5)3=h(x).(g \circ f)(x) = g(2x - 5) = (2x - 5)^3 = h(x).

Check by recomposing: substitute ff‘s formula into gg and confirm it rebuilds hh exactly, which it does. As with the square-root example, this is one valid decomposition among several; the checkpoint below asks you to find another.

Check your understanding

The lesson decomposes h(x)=(2x−5)3h(x) = (2x - 5)^3 as f(x)=2x−5f(x) = 2x - 5, g(x)=x3g(x) = x^3. Which pair gives a genuinely different valid decomposition, h=g∘fh = g \circ f?

Answer choices

Composing a function with itself

Because composition is associative, composing a function with itself repeatedly is unambiguous, and it has its own notation. Write f2=f∘ff^2 = f \circ f, the small superscript counting how many times ff is applied. Read f2(x)=f(f(x))f^2(x) = f(f(x)): apply ff, then apply ff again to the result.

Here is the notation warning. That superscript clashes with the other thing a superscript can mean, squaring the output, (f(x))2=f(x)⋅f(x)(f(x))^2 = f(x) \cdot f(x). The two are different functions. For f(x)=2x−1f(x) = 2x - 1,

f2(x)=f(f(x))=2(2x−1)−1=4x−3,f^2(x) = f(f(x)) = 2(2x - 1) - 1 = 4x - 3,

while

(f(x))2=(2x−1)2=4x2−4x+1,(f(x))^2 = (2x - 1)^2 = 4x^2 - 4x + 1,

and these are different functions: one is linear, the other is not. Which meaning f2f^2 carries is a matter of convention, and this lesson uses f2f^2 for the composition f∘ff \circ f; when we mean the square of the output we write (f(x))2(f(x))^2. One more warning for the next lesson: there f−1f^{-1} will mean the inverse of ff under composition, never 1f\dfrac{1}{f}. In this lesson’s convention, a superscript on a function’s name is about composition, not multiplication.

Check your understanding

For f(x)=3x+2f(x) = 3x + 2, which expression equals f2(x)f^2(x), meaning f∘ff \circ f?

Answer choices

Transformations were compositions all along

The previous lesson wrote every transformed graph as y=a f(b(x−h))+ky = a\,f\big(b(x - h)\big) + k, split into an inside that acts on the input before ff and an outside that acts on ff‘s output afterward. Name those two pieces as functions, an input map and an output map. The transformed rule is then exactly a composition of three functions: input map, then ff, then output map. That is also why “stretch then shift” gave a different graph from “shift then stretch”: the two output maps did not commute, the same reason g∘f≠f∘gg \circ f \ne f \circ g in general.

One more distinction is worth a sentence. Composition chains two functions, feeding one’s output into the other’s input; adding, subtracting, multiplying, or dividing two functions instead combines their outputs at the same input, (f+g)(x)=f(x)+g(x)(f + g)(x) = f(x) + g(x). They are different operations that happen to both start from two functions.

Common mistakes

Practice

Multiple Choice Questions (MCQ)

Progressively harder sets of questions. Each opens on its own page.

Core practice

Practice problems at the level of the course, to be worked out on paper. Hints one at a time, then the answer or the full worked solution, with your progress kept in this browser.

Core practice Work it out on paper 10 problems Start →
More practice (optional)

Extra sets, as hard as the Challenge set. Each one opens on its own page.

More resources (optional)

Other explanations of this lesson, if you want a second take.

Go deeper (optional)

You can skip this and keep going. Read it if you want to know more.

Why composition is associative

The main lesson states this fact. Here is the proof, which is also why an iterated composition like f∘f∘ff \circ f \circ f needs no parentheses to say what it means.

Composition is associative#

Let ff, gg, and hh be functions, and consider the two ways to group their composition, (h∘g)∘f(h \circ g) \circ f and h∘(g∘f)h \circ (g \circ f). Two functions are equal exactly when they have the same domain, codomain, and values, the equality test from Function Notation, so we check the values, the domains, and the codomains.

For the values, apply each grouping to an input xx and unfold the definition one step at a time. The left grouping gives

((h∘g)∘f)(x)=(h∘g)(f(x))=h(g(f(x))),\big((h \circ g) \circ f\big)(x) = (h \circ g)(f(x)) = h\big(g(f(x))\big),

and the right grouping gives

(h∘(g∘f))(x)=h((g∘f)(x))=h(g(f(x))).\big(h \circ (g \circ f)\big)(x) = h\big((g \circ f)(x)\big) = h\big(g(f(x))\big).

Both collapse to the single expression h(g(f(x)))h(g(f(x))), so the two functions agree wherever they are defined.

For the domains, read off what each grouping requires. Building h(g(f(x)))h(g(f(x))) is legal exactly when x∈dom⁡fx \in \operatorname{dom} f so that f(x)f(x) exists, then f(x)∈dom⁡gf(x) \in \operatorname{dom} g so that g(f(x))g(f(x)) exists, then g(f(x))∈dom⁡hg(f(x)) \in \operatorname{dom} h so that the outer hh applies. These three conditions are the same for both groupings, so the two functions share the same domain. Both also share the same codomain, whatever codomain we declared for hh, since both groupings end by applying hh. By the equality test they are the same function.

Because the grouping never matters, we drop the parentheses and write h∘g∘fh \circ g \circ f.

The identity function, defined carefully

The main lesson uses the identity function informally. Here is why composing with it changes nothing, done carefully: the equality test demands the same domain, codomain, and values, not just a matching formula.

The identity function is neutral for composition#

Let f:A→Bf: A \to B be a function. For any set SS, let id⁡S(x)=x\operatorname{id}_S(x) = x be the identity function on SS, the function with domain SS, codomain SS, and rule “return the input unchanged.” We show f∘id⁡A=ff \circ \operatorname{id}_A = f and id⁡B∘f=f\operatorname{id}_B \circ f = f, taking the identity on the correct set each time.

For every x∈Ax \in A,

(f∘id⁡A)(x)=f(id⁡A(x))=f(x),(f \circ \operatorname{id}_A)(x) = f(\operatorname{id}_A(x)) = f(x),

and f∘id⁡Af \circ \operatorname{id}_A has domain AA and codomain BB, matching ff exactly. So f∘id⁡A=ff \circ \operatorname{id}_A = f.

Likewise, for every x∈Ax \in A,

(id⁡B∘f)(x)=id⁡B(f(x))=f(x),(\operatorname{id}_B \circ f)(x) = \operatorname{id}_B(f(x)) = f(x),

since f(x)∈Bf(x) \in B, the domain of id⁡B\operatorname{id}_B, so the composition is legal for every x∈Ax \in A. And id⁡B∘f\operatorname{id}_B \circ f has domain AA and codomain BB, again matching ff. So id⁡B∘f=f\operatorname{id}_B \circ f = f.

The two sides are not symmetric. On the left, the identity’s codomain is forced: id⁡C∘f=f\operatorname{id}_C \circ f = f only when C=BC = B exactly, since a composite’s codomain always matches its outer function’s, and that has to equal BB. On the right, f∘id⁡A=ff \circ \operatorname{id}_A = f, and so does f∘id⁡Tf \circ \operatorname{id}_T for any set TT that contains AA. The domain of that composite is {x∈T:x∈A}=A\{x \in T : x \in A\} = A either way, since ff‘s own domain restriction already confines things to AA. So id⁡A\operatorname{id}_A is simply the smallest, most natural choice on that side, not the only one that works; the care that matters is on the left.

Composition also has a name for what these two facts give it, together with the fact that it is not generally commutative. When ff, gg, and hh all map one fixed set XX back into itself, composing any two of them gives another function X→XX \to X, id⁡X\operatorname{id}_X serves as the identity, and composition of such functions is associative. Associative, with an identity, but not generally commutative and not guaranteed an inverse for every element: that structure is what algebraists call a monoid. The self-maps of XX under composition form one such monoid. The missing piece, an inverse that undoes a function, is what the next lesson supplies for the functions that have one.

Writing a transformation as a composition

The main lesson states the connection in one paragraph. Here it is in full, with the input and output maps named and a worked example.

Write B(x)=b(x−h)B(x) = b(x - h) for the input map and A(y)=ay+kA(y) = ay + k for the output map, so BB acts on the input before ff and AA acts on ff‘s output afterward. Then

y=A(f(B(x)))=(A∘f∘B)(x).y = A\big(f(B(x))\big) = (A \circ f \circ B)(x).

Reading right to left, BB acts first, then ff, then AA, exactly the story the previous lesson told: the inside happens before ff, and the outside after. Its rule that input maps “run backward because you solve for xx” is also explained: in A∘f∘BA \circ f \circ B the rightmost map BB acts before ff. So to find which xx produces a given input to ff, you must undo BB, that is, solve B(x)=uB(x) = u for xx.

Worked example 5 A transformation read as a composition

Write y=2f(3x−6)+1y = 2f(3x - 6) + 1 in the form A∘f∘BA \circ f \circ B, and identify AA and BB.

Factor the inside so it matches b(x−h)b(x - h), and read the outside directly:

3x−6=3(x−2),soB(x)=3(x−2),A(y)=2y+1.3x - 6 = 3(x - 2), \qquad \text{so} \qquad B(x) = 3(x - 2), \quad A(y) = 2y + 1.

Check that composing them rebuilds the rule:

(A∘f∘B)(x)=A(f(3(x−2)))=2 f(3x−6)+1.(A \circ f \circ B)(x) = A\big(f(3(x - 2))\big) = 2\,f(3x - 6) + 1.

So the transformation is the composition A∘f∘BA \circ f \circ B with B(x)=3(x−2)B(x) = 3(x - 2) acting on the input and A(y)=2y+1A(y) = 2y + 1 acting on the output.

Iterating further: f3f^3 and a general pattern

The main lesson computes f2f^2 for f(x)=2x−1f(x) = 2x - 1. Here is f3f^3, and the general pattern it starts.

Apply ff once more, now to f2(x)=4x−3f^2(x) = 4x - 3:

f3(x)=f(f2(x))=2(4x−3)−1=8x−7.f^3(x) = f(f^2(x)) = 2(4x - 3) - 1 = 8x - 7.

A pattern is visible: each application doubles the coefficient of xx and adjusts the constant to match, giving fn(x)=2nx−(2n−1)f^n(x) = 2^n x - (2^n - 1), which reads 2x−12x - 1, 4x−34x - 3, 8x−78x - 7 for n=1,2,3n = 1, 2, 3. The pattern continues because each new application does the same two things to whatever came before it: double the result and subtract 11. So the coefficient of xx doubles again, and the constant term absorbs one more copy of that same rule. None of these equal the output square (f(x))2=4x2−4x+1(f(x))^2 = 4x^2 - 4x + 1, which is not even linear: iterating a function and squaring its value are unrelated operations that happen to share a symbol.

Combining functions without chaining them

Composition is not the only way to build a new function from two others. You can add, subtract, multiply, or divide two functions by combining their outputs at each input:

(f+g)(x)=f(x)+g(x),(fg)(x)=f(x) g(x),(fg)(x)=f(x)g(x).\begin{aligned} (f + g)(x) &= f(x) + g(x), \qquad (fg)(x) = f(x)\,g(x), \\ \left(\frac{f}{g}\right)(x) &= \frac{f(x)}{g(x)}. \end{aligned}

These evaluate both functions at the same xx and combine the two numbers, so both functions see the original input. Composition is different in kind: it chains them, feeding the output of one as the input of the other, so only ff sees xx and gg sees f(x)f(x). The domain rules differ to match. A sum or product is defined where both functions are, dom⁡f∩dom⁡g\operatorname{dom} f \cap \operatorname{dom} g, the very intersection that is the wrong answer for composition. A quotient fg\dfrac{f}{g} additionally removes the inputs where g(x)=0g(x) = 0.

A bit of history (optional)

For most of the nineteenth century, mathematicians who studied groups only ever meant one thing: groups of permutations, the different ways to rearrange a list. Combining two rearrangements meant doing one after the other, which is composition and nothing else. Nobody thought of it as an example of something more general, because nothing else looked like it.

Arthur Cayley, an English mathematician who earned his living as a lawyer, changed that in 1854. His short papers on groups kept the laws an operation obeys and let go of the objects obeying them. In place of a recipe for shuffling a list, he printed a table of results. The same laws turned out to describe things with nothing else in common, such as square blocks of numbers under their own rule for multiplying.

This lesson takes the identical step with composition, a procedure you already knew. It names the laws that procedure obeys: associative, carrying an identity, and, among functions from one set to itself, not generally commutative.