Putting One Function After Another
A function sends each element of its domain to one element of its codomain. If the outputs of one function can be used as inputs to another, the two mappings can be performed in sequence. The resulting function is called a composition. This construction uses the domain, codomain, and range distinctions established in Domain, Codomain, and Range: it is not enough for two formulas to look compatible; the sets specified for the functions matter.
Suppose \(f:A\to B\) and \(g:B\to C\). Starting with \(a\in A\), first apply \(f\), obtaining \(f(a)\in B\). That output belongs to the domain of \(g\), so \(g\) can then be applied to it. The final output is \(g(f(a))\in C\).
Definition (Composition of Functions). Let \(f:A\to B\) and \(g:B\to C\) be functions. The composition of \(f\) followed by \(g\), denoted \(g\circ f\), is the function from \(A\) to \(C\) defined by $$ (g\circ f)(a)=g(f(a))\qquad\text{for every }a\in A. $$ The function \(f\) is applied first; \(g\) is applied second. The notation is read “\(g\) composed with \(f\).”
The order in the notation can initially seem backward. Read the expression from the inside out: in \(g(f(a))\), the input \(a\) goes into \(f\), and the result from \(f\) goes into \(g\). The domain of the composition is the domain of the first function, \(A\), and its codomain is the codomain of the second function, \(C\).
Why a Composition Is a Function
A definition of a function requires every domain element to have one and only one assigned output in the codomain. The formula \(a\mapsto g(f(a))\) meets these requirements because \(f\) assigns a unique value \(f(a)\in B\), and \(g\) assigns a unique value in \(C\) to that element of \(B\).
Theorem (Composition Is a Function). If \(f:A\to B\) and \(g:B\to C\) are functions, then \(g\circ f\) is a function from \(A\) to \(C\).
Proof. For each \(a\in A\), the fact that \(f\) is a function from \(A\) to \(B\) gives exactly one value \(b=f(a)\in B\). Since \(g\) is a function with domain \(B\), there is exactly one value \(c=g(b)\in C\). Thus for each \(a\in A\), the rule \(a\mapsto g(f(a))\) assigns exactly one element of \(C\). This is precisely the requirement for a function from \(A\) to \(C\), so \(g\circ f:A\to C\). \(\square\)
This proof also works when \(A\) is empty. In that case there are no inputs for which an output must be assigned, and the function requirement is satisfied. No extra assumption that the domain is nonempty is needed.
Evaluating a Composition Carefully
To calculate a composition, substitute the output of the first function into the rule for the second. Keep the functions’ names and roles visible until the substitution is complete. In particular, \(g\circ f\) and \(f\circ g\) are different expressions, and they may not both be defined: their domains and codomains must match in the appropriate order.
Worked Example: Composing a Polynomial with a Square Root
Define \(f:\mathbb R\to[1,\infty)\) by \(f(x)=x^2+1\), and define \(g:[1,\infty)\to[0,\infty)\) by \(g(y)=\sqrt{y-1}\). These functions are compatible because the codomain of \(f\) is the domain of \(g\). For \(x\in\mathbb R\), $$ (g\circ f)(x)=g(f(x)) =\sqrt{(x^2+1)-1} =\sqrt{x^2} =|x|. $$ The composition has domain \(\mathbb R\) and codomain \([0,\infty)\), as prescribed by the first domain and second codomain.
The equality \(\sqrt{x^2}=|x|\) is important: it is not always equal to \(x\), since \(x\) may be negative. For example, \((g\circ f)(-4)=4\). The intermediate value is \(f(-4)=17\), which is in the domain of \(g\), and \(g(17)=4\).
Worked Example: Order Changes the Result
Let \(f:\mathbb R\to\mathbb R\) be \(f(x)=2x-1\), and let \(g:\mathbb R\to\mathbb R\) be \(g(x)=x^2\). Both orders of composition are defined. Applying \(f\) first gives $$ (g\circ f)(x)=g(2x-1)=(2x-1)^2 =4x^2-4x+1. $$ Applying \(g\) first gives $$ (f\circ g)(x)=f(x^2)=2x^2-1. $$ At \(x=0\), these values are \(1\) and \(-1\), respectively. Hence \(g\circ f\ne f\circ g\).
The functions have not changed; only their order has changed. The first composition squares the value produced by \(f\), while the second applies \(f\) to the square of the original input. This illustrates that composition is generally not commutative.
Worked Example: Composition on Finite Sets
Let \(A=\{u,v,w\}\), \(B=\{1,2,3\}\), and \(C=\{\alpha,\beta\}\). Define \(f:A\to B\) and \(g:B\to C\) by $$ f(u)=2,\quad f(v)=1,\quad f(w)=3, \qquad g(1)=\alpha,\quad g(2)=\beta,\quad g(3)=\beta. $$ The composition sends each input through both assignments: $$ (g\circ f)(u)=g(2)=\beta,\qquad (g\circ f)(v)=g(1)=\alpha,\qquad (g\circ f)(w)=g(3)=\beta. $$ Thus \(g\circ f:A\to C\) has range \(\{\alpha,\beta\}\). The table shows explicitly that the output of \(f\) is the input used for \(g\).
| Input \(a\) | Intermediate value \(f(a)\) | Final value \(g(f(a))\) |
|---|---|---|
| \(u\) | \(2\) | \(\beta\) |
| \(v\) | \(1\) | \(\alpha\) |
| \(w\) | \(3\) | \(\beta\) |
The Range of a Composition
The composition can reach only values that \(g\) assigns to outputs actually attained by \(f\). The range of \(f\) records precisely those intermediate outputs. If \(S\subseteq B\), write \(g(S)=\{g(b):b\in S\}\) for the set of values of \(g\) on elements of \(S\). With this notation, the range of \(g\circ f\) is the image under \(g\) of the range of \(f\).
Theorem (Range of a Composition). Let \(f:A\to B\) and \(g:B\to C\). Then $$ \operatorname{ran}(g\circ f)=g(\operatorname{ran}(f)). $$
Proof. We prove equality by showing that each set is a subset of the other, using the Equality by Double Inclusion Theorem. First let \(c\in\operatorname{ran}(g\circ f)\). By the definition of range, there exists \(a\in A\) such that \(c=(g\circ f)(a)=g(f(a))\). Since \(f(a)\in\operatorname{ran}(f)\), the value \(g(f(a))\) belongs to \(g(\operatorname{ran}(f))\). Therefore \(c\in g(\operatorname{ran}(f))\).
Conversely, let \(c\in g(\operatorname{ran}(f))\). By the definition of the image of a set, there exists \(b\in\operatorname{ran}(f)\) such that \(c=g(b)\). Since \(b\in\operatorname{ran}(f)\), there exists \(a\in A\) with \(f(a)=b\). Therefore \(c=g(b)=g(f(a))=(g\circ f)(a)\), so \(c\in\operatorname{ran}(g\circ f)\). The two inclusions prove the equality. \(\square\)
The theorem distinguishes the codomain from the range. The codomain of \(g\circ f\) is \(C\), but its range may be a proper subset of \(C\). In the finite-set example, every element of \(C\) happened to be attained. If \(g(1)\) and \(g(2)\) had both equalled \(\alpha\), while \(f\) never took the value \(3\), then \(\beta\) would not be in the range of the composition.
Properties Passed Through Composition
Composition also gives a useful relationship between injectivity and the component functions. If both component functions are injective, then their composition is injective. There is also a one-way converse: if \(g\circ f\) is injective, then \(f\) must be injective. Injectivity of \(g\) does not follow from injectivity of the composition, because \(g\) may identify values in \(B\) that \(f\) never produces.
Theorem (Injectivity of a Composition). Let \(f:A\to B\) and \(g:B\to C\). If \(f\) and \(g\) are injective, then \(g\circ f\) is injective. Moreover, if \(g\circ f\) is injective, then \(f\) is injective.
Proof. First suppose that \(f\) and \(g\) are injective. Let \(x,y\in A\), and suppose \((g\circ f)(x)=(g\circ f)(y)\). By the definition of composition, this means \(g(f(x))=g(f(y))\). Since \(g\) is injective and \(f(x),f(y)\in B\), it follows that \(f(x)=f(y)\). Since \(f\) is injective, \(x=y\). Thus the equal-output criterion for injectivity shows that \(g\circ f\) is injective.
Now suppose instead that \(g\circ f\) is injective. Let \(x,y\in A\) and suppose \(f(x)=f(y)\). Applying \(g\) to these equal elements gives \(g(f(x))=g(f(y))\), or \((g\circ f)(x)=(g\circ f)(y)\). Injectivity of \(g\circ f\) then gives \(x=y\). Therefore \(f\) is injective. \(\square\)
For surjectivity, the Composition of Surjective Functions theorem established in Surjective Functions states that if both component functions are surjective, then their composition is surjective. Together with the injectivity result just proved, it follows that the composition of two bijections is bijective. The hypotheses matter: a composition can be bijective even when an outer function is not injective on its entire domain, if the intermediate values that \(f\) reaches avoid the part where \(g\) fails to be injective.
Associativity and a Reliable Workflow
Suppose \(f:A\to B\), \(g:B\to C\), and \(h:C\to D\). Both \((h\circ g)\circ f\) and \(h\circ(g\circ f)\) are defined from \(A\) to \(D\). Associativity says that these functions are equal. It does not say that composition commutes; the order \(f\), then \(g\), then \(h\) is unchanged.
Theorem (Associativity of Composition). If \(f:A\to B\), \(g:B\to C\), and \(h:C\to D\), then $$ (h\circ g)\circ f=h\circ(g\circ f). $$
Proof. Both sides are functions from \(A\) to \(D\). Let \(a\in A\) be arbitrary. Using the definition of composition, $$ ((h\circ g)\circ f)(a) =(h\circ g)(f(a)) =h(g(f(a))). $$ For the other side, $$ (h\circ(g\circ f))(a) =h((g\circ f)(a)) =h(g(f(a))). $$ Thus the two functions have equal values at every \(a\in A\). By the Pointwise Equality Criterion for functions, they are equal. \(\square\)
Associativity means a chain of functions may be grouped in whichever way makes a calculation convenient, while preserving the order in which each function acts. For a longer chain, parentheses can be omitted once compatibility and order are clear. A useful check before calculating any composition is:
A formula can be composed only after its stated domains and codomains are checked. For instance, the rule \(x\mapsto\sqrt{x}\) can be used after a function whose outputs lie in \([0,\infty)\), but it cannot be applied to negative intermediate values as a real-valued function. Similarly, if \(f:A\to B\) and \(g:D\to C\) are specified with \(B\ne D\), the standard composition \(g\circ f\) is not automatically defined. It is enough in some settings that \(\operatorname{ran}(f)\subseteq D\), but the domain condition must still be made explicit.
Check Your Understanding
- If \(f:A\to B\) and \(g:B\to C\), what are the domain and codomain of \(g\circ f\), and which function is applied first?
- For \(f(x)=x+4\) and \(g(x)=x^2\), calculate \((g\circ f)(x)\) and \((f\circ g)(x)\). Are they equal?
- State the relationship between \(\operatorname{ran}(g\circ f)\) and \(\operatorname{ran}(f)\).
- If \(g\circ f\) is injective, what can be concluded about \(f\)? Explain the role of the equal-output criterion.
- What does associativity allow you to change about a composition of three functions, and what does it not allow you to change?