Composition of Functions

Composing two functions means feeding the output of one as the input of the other. This operation, denoted $f \circ g$, is fundamental to calculus (especially the Chain Rule) and to computer science, where it models the chaining of procedures.

Quick Reference

Concept Formula / Rule
Composite function $(f \circ g)(x) = f(g(x))$
Domain of $f \circ g$ $\{x \in \operatorname{Dom}(g) \mid g(x) \in \operatorname{Dom}(f)\}$
Order matters $f \circ g \neq g \circ f$ in general
Associativity $f \circ g \circ h = (f \circ g) \circ h = f \circ (g \circ h)$

What Is Composition?

If $f(x) = \sqrt{x+1}$ and $g(x) = x^2$, we can define a new function $h$ by substituting $g(x)$ into $f$:

$h(x) = f(g(x)) = f(x^2) = \sqrt{x^2 + 1}.$

In general, to form the composite $f \circ g$, we start with $x$ in the domain of $g$, apply $g$ to get $g(x)$, and then apply $f$ to that result, obtaining $f(g(x))$. This requires $g(x)$ to lie in the domain of $f$.

Machine diagram showing input x through g then f to produce f(g(x)).
(a) Machine diagram: $x \to g \to g(x) \to f \to f(g(x))$.
Arrow diagram showing the mapping from x to g(x) to f(g(x)).
(b) Arrow diagram for $f \circ g$.

Let $f$ and $g$ be two functions. The composite function $f \circ g$ is defined by

$(f \circ g)(x) = f(g(x)),$

for all $x$ in the domain of $g$ for which $g(x)$ lies in the domain of $f$.

The domain of $f \circ g$ is:

$\operatorname{Dom}(f \circ g) = \{x \mid x \in \operatorname{Dom}(g) \text{ and } g(x) \in \operatorname{Dom}(f)\}.$

Order matters: $(f \circ g)(x) = f(g(x))$ and $(g \circ f)(x) = g(f(x))$ are generally different functions.

Examples

Let $f(x) = \sqrt{x}$ and $g(x) = 1 + x^2$. Find $f \circ g$ and $g \circ f$ with their domains.

Solution $\operatorname{Dom}(f) = [0, \infty)$, $\operatorname{Dom}(g) = \mathbb{R}$. $f \circ g$: $(f \circ g)(x) = f(g(x)) = f(1 + x^2) = \sqrt{1 + x^2}.$ Since $1 + x^2 \geq 1 > 0$ for all real $x$, $g(x)$ always lies in $\operatorname{Dom}(f)$. So $\operatorname{Dom}(f \circ g) = \mathbb{R}$. $g \circ f$: $(g \circ f)(x) = g(f(x)) = g(\sqrt{x}) = 1 + (\sqrt{x})^2 = 1 + x \quad (x \geq 0).$ $f$ is defined only for $x \geq 0$, and $f(x) = \sqrt{x} \geq 0$ always lies in $\operatorname{Dom}(g) = \mathbb{R}$. So $\operatorname{Dom}(g \circ f) = [0, \infty)$.

If $f(x) = \dfrac{x+1}{x-1}$, find $f \circ f$ and its domain.

Solution $(f \circ f)(x) = f(f(x)) = f\!\left(\frac{x+1}{x-1}\right) = \frac{\dfrac{x+1}{x-1} + 1}{\dfrac{x+1}{x-1} - 1} = \frac{\dfrac{2x}{x-1}}{\dfrac{2}{x-1}} = \frac{2x}{2} = x.$ The formula simplifies to $x$, but the domain is not all of $\mathbb{R}$. We need:
  1. $x \in \operatorname{Dom}(f)$: $x \neq 1$.
  2. $f(x) \in \operatorname{Dom}(f)$: $\frac{x+1}{x-1} \neq 1$, i.e., $x + 1 \neq x - 1$, i.e., $-1 \neq 1$ (always true).
So the second condition imposes no additional restriction, and $\operatorname{Dom}(f \circ f) = \mathbb{R} - \{1\}$.

Decomposing a Function

Any complex function can often be written as a composition of simpler ones. This skill is essential for the Chain Rule in calculus.

Find $f$ and $g$ such that $h = f \circ g$ where $h(x) = \cos(x^2)$.

Solution To compute $h(x)$, we first square $x$, then take the cosine. So: $g(x) = x^2 \quad \text{(square the input)}, \qquad f(x) = \cos x \quad \text{(take cosine)}.$ Check: $(f \circ g)(x) = f(x^2) = \cos(x^2) = h(x)$. ✓

Find $f$, $g$, $h$ such that $F = f \circ g \circ h$ where $F(x) = \sqrt{|x| + 3}$.

Solution To compute $F(x)$: first take the absolute value, then add 3, then take the square root. $h(x) = |x|, \qquad g(x) = x + 3, \qquad f(x) = \sqrt{x}.$ Check: $(f \circ g \circ h)(x) = f(g(|x|)) = f(|x| + 3) = \sqrt{|x| + 3} = F(x)$. ✓

Composing Three or More Functions

Composition is associative: $f \circ g \circ h = (f \circ g) \circ h = f \circ (g \circ h)$. You can group them in any order when composing more than two functions.

For example, $F(x) = \dfrac{1}{2 + x^2}$ can be written as $F = f \circ g \circ h$ where $h(x) = x^2$, $g(x) = x + 2$, $f(x) = \dfrac{1}{x}$.

Frequently Asked Questions

Is composition commutative? Generally no. $f \circ g$ and $g \circ f$ are usually different functions. For example, with $f(x) = x + 1$ and $g(x) = x^2$: $(f \circ g)(x) = x^2 + 1$ but $(g \circ f)(x) = (x+1)^2$. These are not equal.

How do I find the domain of a composite function? First, find $\operatorname{Dom}(g)$. Then, from those $x$-values, keep only those where $g(x)$ falls inside $\operatorname{Dom}(f)$. The intersection of these two restrictions is $\operatorname{Dom}(f \circ g)$.

Why is composition important in calculus? The Chain Rule in differential calculus says: if $h = f \circ g$, then $h'(x) = f'(g(x)) \cdot g'(x)$. To apply the Chain Rule, you need to recognize a function as a composition and identify its inner and outer parts.

Can a function be composed with itself? Yes. $f \circ f$ is called the iterate of $f$. For example, if $f(x) = x^2$, then $(f \circ f)(x) = (x^2)^2 = x^4$. Iterates appear in dynamical systems and the study of fractals.