Composition of Functions

Suppose you convert a temperature reading in Celsius to Fahrenheit. Then you convert that Fahrenheit reading into a "feels like" index. Can you skip the middle step? Can you build one direct function from Celsius straight to the index? This is the basic question answered by composition of functions. It means gluing two functions end to end, output feeding into input, to build a new function. Composition matters because it is the algebraic glue of mathematics. Groups, categories, dynamical systems, and algorithms are all built from objects (sets, spaces, states). They are also built from arrows (functions, morphisms, transformations) that combine via composition. Understanding exactly when composition is associative is essential. So is understanding how it interacts with injectivity and surjectivity. This is a prerequisite for everything from group theory to the theory of computation. The big picture is that composition turns functions into a kind of algebra. Once you fix a set AA, the functions from AA to AA form a system. This system has an associative "multiplication" (composition) and an identity element (the identity function). This is the seed of the definition of a monoid. When we restrict to bijections, it becomes the seed of a group — the Symmetric Group on AA. Composition also transports structural properties. If two functions are each injective, their composite is injective. If each is surjective, the composite is surjective. The converses are subtler and false in general. This is exactly where the interesting mathematics lives. This article gives the precise definition of composition. It proves that composition is associative. It proves the injectivity/surjectivity preservation theorem in full. It shows with explicit counterexamples why each hypothesis is needed. It builds directly on Bijections and Inverse Functions. It also sets up the language needed later for Images and Preimages of composed functions.

Key takeaways
  • Composition g∘fg \circ f feeds the output of ff into gg; it is defined only when ff's codomain matches gg's domain.
  • Composition of functions is always associative: (h∘g)∘f=h∘(g∘f)(h \circ g) \circ f = h \circ (g \circ f).
  • Composition is generally not commutative: g∘fg \circ f and f∘gf \circ g may not even both be defined, let alone equal.
  • If ff and gg are both injective, g∘fg \circ f is injective; if both surjective, g∘fg \circ f is surjective; if both bijective, so is g∘fg \circ f.
  • The converse direction is asymmetric: injectivity of g∘fg \circ f forces ff injective (not necessarily gg); surjectivity of g∘fg \circ f forces gg surjective (not necessarily ff).
  • Composing with the identity function on either side leaves a function unchanged, making identities the neutral elements of this algebra.
  • Key idea: composition is function-application chained through a shared codomain/domain interface, and it respects associativity and (one-directionally) injectivity/surjectivity.

Where this lives today

Composition of functions is the operational glue behind the Symmetric Group: the set of all bijections from a set AA to itself, under composition, forms a group precisely because of the Theorem proved here — associativity holds automatically, the identity function is the neutral element by part (5), and part (4) guarantees the composite of two bijections is again a bijection (closure), while inverses exist by the theory of Bijections and Inverse Functions. This single algebraic structure underlies the study of permutations, Galois theory, and crystallographic symmetry groups in physics and chemistry. In computer science, function composition is the semantic backbone of functional programming: pipelines built from `map`, `filter`, and user-defined transformations are literally composites of functions, and compilers rely on associativity to reorder or fuse composed operations for optimization without changing program behavior. In category theory, the associativity and identity laws proved above for composition of functions are abstracted into the defining axioms of a category, where "functions" are replaced by arbitrary morphisms between objects — matrices under multiplication, continuous maps between topological spaces, or measurable maps between probability spaces are all instances. In analysis, the chain rule for differentiation is precisely a statement about how differentiability interacts with composition, (g∘f)′(a)=g′(f(a))⋅f′(a)(g\circ f)'(a) = g'(f(a))\cdot f'(a), and the injectivity-preservation fact underlies the construction of inverse functions locally (via the Inverse Function Theorem). In cryptography, block ciphers are built as long composites of simple, invertible round functions; the fact that a composite of bijections is a bijection (part 4 of the Theorem) is exactly what guarantees that encryption remains reversible after many rounds, while associativity guarantees the encryption pipeline can be re-associated or parallelized without changing the ciphertext produced. In statistics and measure theory, composing a random variable (a measurable function) with a further measurable function produces a new random variable, and questions about which distributional properties are preserved under such composition parallel exactly the injectivity/surjectivity analysis given here.

Ask the tutor: If g∘fg \circ f is both injective and surjective, exactly which of ff and gg can I conclude are individually bijective, and what extra assumption would let me conclude both are?

DefinitionComposition of Functions

Let AA, BB, CC be sets, let f:A→Bf : A \to B be a function, and let g:B→Cg : B \to C be a function. The composition of gg with ff, written g∘fg \circ f, is the function

g∘f:A→C,(g∘f)(a)=g(f(a))for all a∈A.g \circ f : A \to C, \qquad (g \circ f)(a) = g\big(f(a)\big) \quad \text{for all } a \in A.

More formally, viewing ff and gg as sets of ordered pairs (subsets of A×BA \times B and B×CB \times C respectively), the composite is the set

g∘f={ (a,c)∈A×C∣∃ b∈B, (a,b)∈f and (b,c)∈g }.g \circ f = \{\, (a,c) \in A \times C \mid \exists\, b \in B,\ (a,b) \in f \text{ and } (b,c) \in g \,\}.

This is well defined as a function A→CA \to C: for each a∈Aa \in A there is exactly one b=f(a)∈Bb = f(a) \in B (since ff is a function), and exactly one c=g(b)∈Cc = g(b) \in C (since gg is a function), so exactly one pair (a,c)(a,c) belongs to g∘fg \circ f. The composition g∘fg \circ f is defined precisely when the codomain of ff equals the domain of gg.

Intuition

Think of a function as a machine with an input slot and an output chute, labeled by its domain and codomain. Composition is physically bolting two machines together: the output chute of the first machine (ff) is welded to the input slot of the second (gg), so that whatever comes out of ff immediately becomes the input fed into gg. The welding only makes physical sense if the shapes match — the codomain of ff must be (a subset of, or exactly) the domain of gg. That is why g∘fg \circ f requires f:A→Bf: A \to B and g:B→Cg : B \to C to share the middle set BB. The notation g∘fg \circ f is read "gg after ff" or "gg compose ff", and it is written in the opposite order from how you'd describe the process in English: you apply ff first, then gg, but ff appears on the right, closest to the input variable aa in (g∘f)(a)=g(f(a))(g\circ f)(a) = g(f(a)). This ordering convention matches ordinary function notation and is the price paid for writing g(f(a))g(f(a)) instead of (a)fg(a)f g. Associativity says that when you chain three machines ff, gg, hh, it does not matter whether you first bolt ff to gg and then attach hh, or first bolt gg to hh and then prepend ff — the final three-stage machine is identical either way. This is not a deep fact about functions specifically; it follows because both sides compute h(g(f(a)))h(g(f(a))) pointwise. But it is exactly what allows us to write h∘g∘fh \circ g \circ f without parentheses, and it is the algebraic bedrock underlying associative structures throughout mathematics, from matrix multiplication to the composition law in a category.

Figure
Composing f:A→Bf:A\to B and g:B→Cg:B\to C: the middle set BB is the shared interface
A B C a f(a) g(f(a)) g ∘ f f g
TheoremAssociativity and Preservation Theorem for Composition

Let A,B,C,DA, B, C, D be sets.

(1) Associativity. If f:A→Bf : A \to B, g:B→Cg : B \to C, and h:C→Dh : C \to D are functions, then

(h∘g)∘f  =  h∘(g∘f)(h \circ g) \circ f \;=\; h \circ (g \circ f)

as functions from AA to DD.

(2) Preservation of injectivity. If f:A→Bf : A \to B and g:B→Cg : B \to C are both injective, then g∘f:A→Cg \circ f : A \to C is injective.

(3) Preservation of surjectivity. If f:A→Bf : A \to B and g:B→Cg : B \to C are both surjective, then g∘f:A→Cg \circ f : A \to C is surjective.

(4) Preservation of bijectivity. If f:A→Bf : A \to B and g:B→Cg : B \to C are both bijective, then g∘f:A→Cg \circ f : A \to C is bijective.

(5) Identity laws. If f:A→Bf : A \to B and idA:A→A\mathrm{id}_A : A \to A, idB:B→B\mathrm{id}_B : B \to B are the identity functions, then

f∘idA=f=idB∘f.f \circ \mathrm{id}_A = f = \mathrm{id}_B \circ f.
Proof

We prove each part in turn. Throughout, we use the definition of function equality: two functions with the same domain and codomain are equal exactly when they agree at every point of the domain (this is the extensionality criterion for functions).

Proof of (1), by direct computation (pointwise verification). Both (h∘g)∘f(h \circ g) \circ f and h∘(g∘f)h \circ (g \circ f) are functions from AA to DD: indeed g∘f:A→Cg \circ f : A \to C is a well-defined function by the Definition above (applied to f:A→Bf: A\to B, g:B→Cg: B \to C), so h∘(g∘f):A→Dh \circ (g \circ f) : A \to D is well defined (applying the Definition again to g∘f:A→Cg \circ f : A \to C and h:C→Dh : C \to D); symmetrically h∘g:B→Dh \circ g : B \to D is well defined, so (h∘g)∘f:A→D(h\circ g) \circ f : A \to D is well defined. Thus both sides have the same domain AA and codomain DD, and it suffices to check they agree at every a∈Aa \in A. Fix a∈Aa \in A. By the definition of composition applied twice,

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

Likewise,

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

Both expressions equal h(g(f(a)))h(g(f(a))), the single element of DD obtained by applying ff, then gg, then hh to aa. Since a∈Aa \in A was arbitrary, the two functions agree everywhere on AA, hence are equal. ■\blacksquare

Proof of (2), direct proof from the definition of injective. Recall g∘f:A→Cg \circ f : A \to C is injective means: for all a1,a2∈Aa_1, a_2 \in A, if (g∘f)(a1)=(g∘f)(a2)(g\circ f)(a_1) = (g \circ f)(a_2) then a1=a2a_1 = a_2. Let a1,a2∈Aa_1, a_2 \in A with (g∘f)(a1)=(g∘f)(a2)(g \circ f)(a_1) = (g \circ f)(a_2). By definition of composition,

g(f(a1))=g(f(a2)).g(f(a_1)) = g(f(a_2)).

Since g:B→Cg : B \to C is injective and f(a1),f(a2)∈Bf(a_1), f(a_2) \in B, this equality of images under gg forces

f(a1)=f(a2).f(a_1) = f(a_2).

Since f:A→Bf : A \to B is injective and a1,a2∈Aa_1, a_2 \in A, this equality of images under ff forces a1=a2a_1 = a_2. As a1,a2a_1, a_2 were arbitrary elements of AA satisfying (g∘f)(a1)=(g∘f)(a2)(g\circ f)(a_1)=(g\circ f)(a_2), we conclude g∘fg \circ f is injective. ■\blacksquare

Proof of (3), direct proof from the definition of surjective. Recall g∘f:A→Cg \circ f : A \to C is surjective means: for every c∈Cc \in C, there exists a∈Aa \in A with (g∘f)(a)=c(g \circ f)(a) = c. Let c∈Cc \in C be arbitrary. Since g:B→Cg : B \to C is surjective, there exists b∈Bb \in B with g(b)=cg(b) = c. Since f:A→Bf : A \to B is surjective, there exists a∈Aa \in A with f(a)=bf(a) = b. Then

(g∘f)(a)=g(f(a))=g(b)=c.(g \circ f)(a) = g(f(a)) = g(b) = c.

Thus for every c∈Cc \in C we have exhibited a∈Aa \in A with (g∘f)(a)=c(g\circ f)(a) = c, so g∘fg \circ f is surjective. ■\blacksquare

Proof of (4). Immediate from (2) and (3): if ff and gg are both injective and both surjective, then by (2) g∘fg \circ f is injective and by (3) g∘fg\circ f is surjective, hence bijective by definition of bijective. ■\blacksquare

Proof of (5), by direct computation. The function f∘idAf \circ \mathrm{id}_A has domain AA and codomain BB (since idA:A→A\mathrm{id}_A : A \to A and f:A→Bf : A \to B share the middle set AA), matching the domain and codomain of ff. For every a∈Aa \in A,

(f∘idA)(a)=f(idA(a))=f(a),(f \circ \mathrm{id}_A)(a) = f(\mathrm{id}_A(a)) = f(a),

using the definition idA(a)=a\mathrm{id}_A(a) = a. Since the two functions agree at every point of the shared domain AA, f∘idA=ff \circ \mathrm{id}_A = f. Similarly idB∘f\mathrm{id}_B \circ f has domain AA and codomain BB, and for every a∈Aa \in A,

(idB∘f)(a)=idB(f(a))=f(a),(\mathrm{id}_B \circ f)(a) = \mathrm{id}_B(f(a)) = f(a),

using idB(y)=y\mathrm{id}_B(y) = y for y=f(a)∈By = f(a) \in B. Hence idB∘f=f\mathrm{id}_B \circ f = f. ■\blacksquare

∎

Worked examples

Example 1 (asymmetric domains, tracking injectivity and surjectivity through a chain). Let A={1,2,3}A = \{1,2,3\}, B={p,q,r,s}B = \{p,q,r,s\}, C={x,y}C = \{x,y\}. Define

f:A→B,f(1)=p, f(2)=q, f(3)=r,f : A \to B, \quad f(1)=p,\ f(2)=q,\ f(3)=r,
g:B→C,g(p)=x, g(q)=x, g(r)=y, g(s)=y.g : B \to C, \quad g(p)=x,\ g(q)=x,\ g(r)=y,\ g(s)=y.

Here ff is injective (three distinct inputs, three distinct outputs p,q,rp,q,r) but not surjective onto BB (the element ss is missed). The function gg is surjective onto CC (both xx and yy are hit) but not injective (pp and qq both map to xx). Compute g∘f:A→Cg \circ f : A \to C:

(g∘f)(1)=g(p)=x,(g∘f)(2)=g(q)=x,(g∘f)(3)=g(r)=y.(g\circ f)(1) = g(p) = x, \quad (g\circ f)(2) = g(q) = x, \quad (g\circ f)(3) = g(r) = y.

Observe (g∘f)(1)=(g∘f)(2)=x(g\circ f)(1) = (g \circ f)(2) = x with 1≠21 \neq 2, so g∘fg \circ f is not injective, even though ff alone is injective. This is consistent with the Theorem: preservation of injectivity requires both ff and gg injective, and here gg fails to be injective, so there is no contradiction. It also illustrates the asymmetric converse fact stated in the summary: since g∘fg \circ f is (in this example) not injective, the theorem gives no information forcing gg to be injective — indeed gg is not. Meanwhile g∘fg \circ f is surjective onto C={x,y}C = \{x,y\} (both xx and yy occur as values), consistent with gg being surjective even though ff is not. This example shows the two preservation directions are independent: injectivity of the composite is controlled by injectivity of the inner function chain in a way that critically needs the outer function gg to not collapse distinct values, while surjectivity of the composite is driven by the outer function gg's surjectivity, needing the inner function ff to reach enough of BB to feed gg's full range — but as this example shows, ff need not be surjective onto all of BB, only onto enough of BB to hit a preimage of every element of CC under gg.

Example 2 (verifying associativity concretely with three genuinely different finite functions). Let A={1,2}A = \{1,2\}, B={a,b,c}B = \{a,b,c\}, C={u,v}C = \{u,v\}, D={α,β,γ}D = \{\alpha,\beta,\gamma\}. Define

f:A→B,f(1)=a, f(2)=c,f: A \to B,\quad f(1) = a,\ f(2) = c,
g:B→C,g(a)=v, g(b)=u, g(c)=v,g: B \to C,\quad g(a) = v,\ g(b) = u,\ g(c) = v,
h:C→D,h(u)=α, h(v)=γ.h: C \to D,\quad h(u) = \alpha,\ h(v) = \gamma.

Compute g∘f:A→Cg \circ f : A \to C first:

(g∘f)(1)=g(a)=v,(g∘f)(2)=g(c)=v.(g\circ f)(1) = g(a) = v, \qquad (g \circ f)(2) = g(c) = v.

Then compute h∘(g∘f):A→Dh \circ (g\circ f) : A \to D:

(h∘(g∘f))(1)=h(v)=γ,(h∘(g∘f))(2)=h(v)=γ.\big(h\circ (g\circ f)\big)(1) = h(v) = \gamma, \qquad \big(h \circ (g \circ f)\big)(2) = h(v) = \gamma.

Now compute the other grouping. First h∘g:B→Dh \circ g : B \to D:

(h∘g)(a)=h(v)=γ,(h∘g)(b)=h(u)=α,(h∘g)(c)=h(v)=γ.(h\circ g)(a) = h(v) = \gamma,\quad (h\circ g)(b) = h(u) = \alpha, \quad (h \circ g)(c) = h(v) = \gamma.

Then (h∘g)∘f:A→D(h\circ g)\circ f : A \to D:

((h∘g)∘f)(1)=(h∘g)(a)=γ,((h∘g)∘f)(2)=(h∘g)(c)=γ.\big((h\circ g)\circ f\big)(1) = (h\circ g)(a) = \gamma, \qquad \big((h\circ g)\circ f\big)(2) = (h \circ g)(c) = \gamma.

Both computations give the function A→DA \to D sending 1↦γ1 \mapsto \gamma and 2↦γ2 \mapsto \gamma, confirming

h∘(g∘f)=(h∘g)∘fh \circ (g \circ f) = (h \circ g) \circ f

in this concrete case, exactly as the Theorem guarantees. Note also that g∘fg \circ f collapses 11 and 22 to the common value vv — since f(1)=a≠c=f(2)f(1)=a \ne c=f(2) but g(a)=g(c)=vg(a)=g(c)=v — again illustrating that g∘fg \circ f can fail injectivity even when ff is injective, because gg itself is not injective on {a,c}\{a,c\}.

CounterexampleWhy both functions must be injective (or both surjective) — dropping one hypothesis

The Theorem's parts (2) and (3) require injectivity (respectively surjectivity) of both ff and gg. We show that dropping the hypothesis on just one of the two functions breaks the conclusion, and moreover that the converse implications fail in general.

Dropping injectivity of gg breaks preservation of injectivity of the composite. Let A={1,2}A = \{1,2\}, B={a,b}B = \{a,b\}, C={x}C = \{x\}, with

f:A→B, f(1)=a, f(2)=b(injective),f : A \to B,\ f(1) = a,\ f(2) = b \quad(\text{injective}),
g:B→C, g(a)=x, g(b)=x(not injective, since a≠b but g(a)=g(b)).g : B \to C,\ g(a) = x,\ g(b) = x \quad(\text{not injective, since } a \ne b \text{ but } g(a)=g(b)).

Then g∘f:A→Cg \circ f : A \to C sends 1↦x1 \mapsto x and 2↦x2 \mapsto x, so (g∘f)(1)=(g∘f)(2)(g\circ f)(1) = (g\circ f)(2) with 1≠21 \ne 2: the composite is not injective, even though ff is injective. This shows the injectivity of ff alone is not sufficient; gg's injectivity is an essential hypothesis in part (2).

Dropping surjectivity of ff breaks preservation of surjectivity of the composite. Let A={1}A = \{1\}, B={a,b}B = \{a,b\}, C={x,y}C = \{x,y\}, with

f:A→B, f(1)=a(not surjective, since b is never hit),f : A \to B,\ f(1) = a \quad (\text{not surjective, since } b \text{ is never hit}),
g:B→C, g(a)=x, g(b)=y(surjective).g : B \to C,\ g(a) = x,\ g(b) = y \quad (\text{surjective}).

Then g∘f:A→Cg \circ f : A \to C sends 1↦x1 \mapsto x, so its range is {x}⊊C\{x\} \subsetneq C: the composite is not surjective, even though gg is surjective. This shows the surjectivity of gg alone is not sufficient; ff's surjectivity is essential in part (3).

The converses of parts (2) and (3) genuinely fail (they are one-directional, not "if and only if"). It is true (and worth proving explicitly, since it is often mis-stated as symmetric) that:

  • If g∘fg \circ f is injective, then ff must be injective — but gg need not be injective.
  • If g∘fg \circ f is surjective, then gg must be surjective — but ff need not be surjective.

Proof that g∘fg\circ f injective forces ff injective: suppose f(a1)=f(a2)f(a_1) = f(a_2). Applying gg to both sides, g(f(a1))=g(f(a2))g(f(a_1)) = g(f(a_2)), i.e. (g∘f)(a1)=(g∘f)(a2)(g\circ f)(a_1) = (g\circ f)(a_2). Since g∘fg \circ f is injective, a1=a2a_1 = a_2. Hence ff is injective. ■\blacksquare Counterexample showing gg need not be injective in that scenario: Let A={1}A=\{1\}, B={a,b}B=\{a,b\}, C={x}C = \{x\}, f(1)=af(1) = a, and g(a)=g(b)=xg(a) = g(b) = x. Then g∘f:A→Cg\circ f: A \to C sends 1↦x1 \mapsto x; since AA has only one element, g∘fg \circ f is vacuously injective (there are no two distinct elements of AA to violate injectivity), yet gg is not injective (g(a)=g(b)g(a) = g(b) with a≠ba \ne b). Proof that g∘fg\circ f surjective forces gg surjective: let c∈Cc \in C. Since g∘fg \circ f is surjective, there is a∈Aa \in A with g(f(a))=cg(f(a)) = c; taking b=f(a)∈Bb = f(a) \in B gives g(b)=cg(b) = c. Hence gg is surjective. ■\blacksquare Counterexample showing ff need not be surjective in that scenario: reuse A={1}A=\{1\}, B={a,b}B=\{a,b\}, C={x}C=\{x\}, f(1)=af(1)=a, g(a)=g(b)=xg(a)=g(b)=x. Then g∘fg\circ f is surjective onto C={x}C=\{x\} (it hits xx), but ff is not surjective onto BB (it never hits bb). Together these examples show the preservation theorem's hypotheses are tight in both directions, and that the correct converse statements are strictly weaker (and asymmetric between ff and gg) than one might first guess.

Scope, edge cases & common traps

SituationWhat holdsWhat can fail
f,gf, g both injectiveg∘fg\circ f injective (proved)—
f,gf, g both surjectiveg∘fg \circ f surjective (proved)—
Only ff injective, gg notg∘fg \circ f need not be injectiveCounterexample above
Only gg surjective, ff notg∘fg \circ f need not be surjectiveCounterexample above
g∘fg \circ f injectiveForces ff injectiveDoes not force gg injective
g∘fg \circ f surjectiveForces gg surjectiveDoes not force ff surjective
f,gf, g both bijectiveg∘fg\circ f bijective; (g∘f)−1=f−1∘g−1(g\circ f)^{-1} = f^{-1}\circ g^{-1}Order reverses in the inverse
Composing three or more functionsAssociative, so parentheses may be droppedOrder of the functions (not grouping) still matters
g∘fg \circ f vs f∘gf \circ gBoth may be defined if A=B=CA=B=CGenerally g∘f≠f∘gg\circ f \ne f \circ g even then

The mathematician behind it

Origins · 1 minCharles Babbage and how this was discovered
CB
Charles Babbage
1791–1871 · early theory of algorithmic composition

Charles Babbage, though most famous as an early designer of mechanical calculating engines, was among the first to think systematically about the "calculus of functions" — treating the algebraic combination of functions, including composition and functional roots (functions ff with f(f(x))=g(x)f(f(x)) = g(x) for given gg), as objects of formal study in the early nineteenth century. His engines, the Difference Engine and the unbuilt Analytical Engine, embodied the idea that a computation could be built by chaining elementary operations — an operational, mechanical ancestor of the abstract notion of composing functions that later became foundational to set theory and to the theory of computation.

History
- 1671–1675: Newton and Leibniz develop calculus; Leibniz's notation for functions and their combinations lays informal groundwork for later formal composition.
- 1830s: Babbage's work on the Analytical Engine treats sequences of operations as composable mechanical procedures, prefiguring composition as an algebraic operation.
- 1854: George Boole's Laws of Thought develops an algebra of operations, part of the broader nineteenth-century push toward treating functions and operations symbolically.
- 1895–1897: Georg Cantor's foundational papers on set theory give the rigorous set-of-ordered-pairs definition of function that makes today's definition of composition precise.
- 1930s: The Bourbaki school and category-theoretic precursors (Eilenberg–Mac Lane's category theory arrives in 1945) crystallize composition and associativity as the defining structural axioms of mathematical "arrows," generalizing function composition to morphisms in any category.
Is g∘fg \circ f always the same as f∘gf \circ g?

No, in general they are not equal, and often only one of them is even defined. Composition g∘fg \circ f requires the codomain of ff to match the domain of gg; the reverse composite f∘gf \circ g requires the codomain of gg to match the domain of ff. Even in the special case A=B=CA=B=C where both composites are defined (e.g. functions from the real numbers to themselves), they typically disagree: with f(x)=x+1f(x)=x+1 and g(x)=2xg(x)=2x, one finds (g∘f)(x)=2x+2(g\circ f)(x) = 2x+2 while (f∘g)(x)=2x+1(f\circ g)(x)=2x+1, which are different functions. Commutativity of composition is the exception, not the rule; it holds in special cases (like ff and gg both being powers of a single function) but should never be assumed.

Why does the domain/codomain matching condition matter so much?

Composition g∘fg\circ f is defined by first evaluating ff at an input, then feeding that output into gg. For g(f(a))g(f(a)) to make sense, f(a)f(a) must actually lie in the domain of gg. If the codomain of ff is not contained in the domain of gg, there could be elements f(a)f(a) for which gg is simply not defined, and g∘fg\circ f would fail to be a well-defined function on all of AA. This is why the formal definition insists that the codomain of ff equal the domain of gg (or, in more flexible treatments, that the range of ff be a subset of the domain of gg). Ignoring this condition is a common source of "composition" that silently produces a partial function rather than a genuine function.

Does associativity mean I can compose functions in any order?

No — associativity only says that the grouping (where you place parentheses) does not matter, not that the order of the functions can be permuted. For h∘g∘fh\circ g \circ f, you must still apply ff first, then gg, then hh; associativity just guarantees (h∘g)∘f(h\circ g)\circ f and h∘(g∘f)h \circ (g\circ f) compute the identical function, so you can drop the parentheses and write h∘g∘fh\circ g\circ f unambiguously. Swapping the order, e.g. considering g∘h∘fg \circ h \circ f instead, generally produces a different function altogether (and might not even be well defined if the domains and codomains do not line up in the new order).

If g∘fg\circ f is a bijection, must ff and gg individually be bijections?

Not necessarily both, though each is constrained. From g∘fg\circ f injective, we can prove ff is injective; from g∘fg \circ f surjective, we can prove gg is surjective. So ff is at least injective and gg is at least surjective. But ff need not be surjective, and gg need not be injective, in general — the earlier counterexample block exhibits exactly such a pair where g∘fg\circ f is a bijection (in fact injective vacuously and surjective) while gg fails injectivity. A clean sufficient condition to get both f,gf,g bijective is to also assume AA, BB, CC are finite sets of the same cardinality, which then forces injective-or-surjective to imply bijective at each stage — but this uses finiteness essentially.

How does composition relate to inverse functions?

If f:A→Bf: A \to B and g:B→Ag: B\to A satisfy g∘f=idAg \circ f = \mathrm{id}_A and f∘g=idBf \circ g = \mathrm{id}_B, then ff and gg are mutually inverse bijections; this is the defining relationship studied in Bijections and Inverse Functions. Composition is the operation used to state what it means to be an inverse, and the preservation-of-bijectivity fact proved here (part 4 of the Theorem) is exactly what guarantees that composing two invertible functions gives another invertible function, with inverse (g∘f)−1=f−1∘g−1(g\circ f)^{-1} = f^{-1}\circ g^{-1} — note the reversed order, which follows from associativity applied to f−1∘g−1∘g∘f=f−1∘idB∘f=f−1∘f=idAf^{-1}\circ g^{-1}\circ g\circ f = f^{-1}\circ \mathrm{id}_B \circ f = f^{-1}\circ f=\mathrm{id}_A.

Why is the order in the notation g∘fg\circ f "backwards" compared to the process order?

The convention (g∘f)(a)=g(f(a))(g\circ f)(a) = g(f(a)) is chosen so that the composite notation matches how nested function application is already written: you read g(f(a))g(f(a)) from the outside in, applying gg to whatever f(a)f(a) turns out to be. Writing g∘fg\circ f mirrors this outside-to-inside reading, with gg written first (leftmost) even though it is applied last (temporally). Some texts and some programming languages (via pipe operators) instead adopt a "diagrammatic" or left-to-right order, writing f;gf;g or f≫gf \gg g to mean "first ff, then gg" with the reading order matching the process order; both conventions are logically equivalent, but it is essential to fix and state which convention is in force, since g∘fg\circ f and f∘gf \circ g genuinely differ.

Does composition make sense for relations that are not functions?

Yes, and the ordered-pairs definition given here generalizes directly: for relations R⊆A×BR \subseteq A\times B and S⊆B×CS \subseteq B \times C, define S∘R={(a,c)∣∃b, (a,b)∈R and (b,c)∈S}S\circ R = \{(a,c) \mid \exists b,\ (a,b)\in R \text{ and } (b,c)\in S\}, exactly the formula given in the Definition above but without requiring uniqueness of bb. Composition of relations is still associative (the same pointwise-style argument works, quantifying over existentially chosen intermediate elements rather than the unique ones forced by functionality), but it need not preserve "function-like" behavior, since S∘RS\circ R may relate a single aa to multiple elements of CC even when RR and SS are each functions is not the case here — this relational generalization is part of what makes composition a template later used for composing preimages and images of sets under a function, in the study of Images and Preimages.

Before this