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 , the functions from to 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 . 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.
- Composition feeds the output of into ; it is defined only when 's codomain matches 's domain.
- Composition of functions is always associative: .
- Composition is generally not commutative: and may not even both be defined, let alone equal.
- If and are both injective, is injective; if both surjective, is surjective; if both bijective, so is .
- The converse direction is asymmetric: injectivity of forces injective (not necessarily ); surjectivity of forces surjective (not necessarily ).
- 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 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, , 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 is both injective and surjective, exactly which of and can I conclude are individually bijective, and what extra assumption would let me conclude both are?
Let , , be sets, let be a function, and let be a function. The composition of with , written , is the function
More formally, viewing and as sets of ordered pairs (subsets of and respectively), the composite is the set
This is well defined as a function : for each there is exactly one (since is a function), and exactly one (since is a function), so exactly one pair belongs to . The composition is defined precisely when the codomain of equals the domain of .
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 () is welded to the input slot of the second (), so that whatever comes out of immediately becomes the input fed into . The welding only makes physical sense if the shapes match — the codomain of must be (a subset of, or exactly) the domain of . That is why requires and to share the middle set . The notation is read " after " or " compose ", and it is written in the opposite order from how you'd describe the process in English: you apply first, then , but appears on the right, closest to the input variable in . This ordering convention matches ordinary function notation and is the price paid for writing instead of . Associativity says that when you chain three machines , , , it does not matter whether you first bolt to and then attach , or first bolt to and then prepend — the final three-stage machine is identical either way. This is not a deep fact about functions specifically; it follows because both sides compute pointwise. But it is exactly what allows us to write without parentheses, and it is the algebraic bedrock underlying associative structures throughout mathematics, from matrix multiplication to the composition law in a category.
Let be sets.
(1) Associativity. If , , and are functions, then
as functions from to .
(2) Preservation of injectivity. If and are both injective, then is injective.
(3) Preservation of surjectivity. If and are both surjective, then is surjective.
(4) Preservation of bijectivity. If and are both bijective, then is bijective.
(5) Identity laws. If and , are the identity functions, then
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 and are functions from to : indeed is a well-defined function by the Definition above (applied to , ), so is well defined (applying the Definition again to and ); symmetrically is well defined, so is well defined. Thus both sides have the same domain and codomain , and it suffices to check they agree at every . Fix . By the definition of composition applied twice,
Likewise,
Both expressions equal , the single element of obtained by applying , then , then to . Since was arbitrary, the two functions agree everywhere on , hence are equal.
Proof of (2), direct proof from the definition of injective. Recall is injective means: for all , if then . Let with . By definition of composition,
Since is injective and , this equality of images under forces
Since is injective and , this equality of images under forces . As were arbitrary elements of satisfying , we conclude is injective.
Proof of (3), direct proof from the definition of surjective. Recall is surjective means: for every , there exists with . Let be arbitrary. Since is surjective, there exists with . Since is surjective, there exists with . Then
Thus for every we have exhibited with , so is surjective.
Proof of (4). Immediate from (2) and (3): if and are both injective and both surjective, then by (2) is injective and by (3) is surjective, hence bijective by definition of bijective.
Proof of (5), by direct computation. The function has domain and codomain (since and share the middle set ), matching the domain and codomain of . For every ,
using the definition . Since the two functions agree at every point of the shared domain , . Similarly has domain and codomain , and for every ,
using for . Hence .
∎Worked examples
Example 1 (asymmetric domains, tracking injectivity and surjectivity through a chain). Let , , . Define
Here is injective (three distinct inputs, three distinct outputs ) but not surjective onto (the element is missed). The function is surjective onto (both and are hit) but not injective ( and both map to ). Compute :
Observe with , so is not injective, even though alone is injective. This is consistent with the Theorem: preservation of injectivity requires both and injective, and here fails to be injective, so there is no contradiction. It also illustrates the asymmetric converse fact stated in the summary: since is (in this example) not injective, the theorem gives no information forcing to be injective — indeed is not. Meanwhile is surjective onto (both and occur as values), consistent with being surjective even though 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 to not collapse distinct values, while surjectivity of the composite is driven by the outer function 's surjectivity, needing the inner function to reach enough of to feed 's full range — but as this example shows, need not be surjective onto all of , only onto enough of to hit a preimage of every element of under .
Example 2 (verifying associativity concretely with three genuinely different finite functions). Let , , , . Define
Compute first:
Then compute :
Now compute the other grouping. First :
Then :
Both computations give the function sending and , confirming
in this concrete case, exactly as the Theorem guarantees. Note also that collapses and to the common value — since but — again illustrating that can fail injectivity even when is injective, because itself is not injective on .
The Theorem's parts (2) and (3) require injectivity (respectively surjectivity) of both and . 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 breaks preservation of injectivity of the composite. Let , , , with
Then sends and , so with : the composite is not injective, even though is injective. This shows the injectivity of alone is not sufficient; 's injectivity is an essential hypothesis in part (2).
Dropping surjectivity of breaks preservation of surjectivity of the composite. Let , , , with
Then sends , so its range is : the composite is not surjective, even though is surjective. This shows the surjectivity of alone is not sufficient; '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 is injective, then must be injective — but need not be injective.
- If is surjective, then must be surjective — but need not be surjective.
Proof that injective forces injective: suppose . Applying to both sides, , i.e. . Since is injective, . Hence is injective. Counterexample showing need not be injective in that scenario: Let , , , , and . Then sends ; since has only one element, is vacuously injective (there are no two distinct elements of to violate injectivity), yet is not injective ( with ). Proof that surjective forces surjective: let . Since is surjective, there is with ; taking gives . Hence is surjective. Counterexample showing need not be surjective in that scenario: reuse , , , , . Then is surjective onto (it hits ), but is not surjective onto (it never hits ). 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 and ) than one might first guess.
Scope, edge cases & common traps
| Situation | What holds | What can fail |
|---|---|---|
| both injective | injective (proved) | — |
| both surjective | surjective (proved) | — |
| Only injective, not | need not be injective | Counterexample above |
| Only surjective, not | need not be surjective | Counterexample above |
| injective | Forces injective | Does not force injective |
| surjective | Forces surjective | Does not force surjective |
| both bijective | bijective; | Order reverses in the inverse |
| Composing three or more functions | Associative, so parentheses may be dropped | Order of the functions (not grouping) still matters |
| vs | Both may be defined if | Generally even then |
The mathematician behind it
Origins · 1 minCharles Babbage and how this was discovered
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 with for given ), 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.
Is always the same as ?
No, in general they are not equal, and often only one of them is even defined. Composition requires the codomain of to match the domain of ; the reverse composite requires the codomain of to match the domain of . Even in the special case where both composites are defined (e.g. functions from the real numbers to themselves), they typically disagree: with and , one finds while , which are different functions. Commutativity of composition is the exception, not the rule; it holds in special cases (like and both being powers of a single function) but should never be assumed.
Why does the domain/codomain matching condition matter so much?
Composition is defined by first evaluating at an input, then feeding that output into . For to make sense, must actually lie in the domain of . If the codomain of is not contained in the domain of , there could be elements for which is simply not defined, and would fail to be a well-defined function on all of . This is why the formal definition insists that the codomain of equal the domain of (or, in more flexible treatments, that the range of be a subset of the domain of ). 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 , you must still apply first, then , then ; associativity just guarantees and compute the identical function, so you can drop the parentheses and write unambiguously. Swapping the order, e.g. considering 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 is a bijection, must and individually be bijections?
Not necessarily both, though each is constrained. From injective, we can prove is injective; from surjective, we can prove is surjective. So is at least injective and is at least surjective. But need not be surjective, and need not be injective, in general — the earlier counterexample block exhibits exactly such a pair where is a bijection (in fact injective vacuously and surjective) while fails injectivity. A clean sufficient condition to get both bijective is to also assume , , 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 and satisfy and , then and 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 — note the reversed order, which follows from associativity applied to .
Why is the order in the notation "backwards" compared to the process order?
The convention is chosen so that the composite notation matches how nested function application is already written: you read from the outside in, applying to whatever turns out to be. Writing mirrors this outside-to-inside reading, with 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 or to mean "first , then " 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 and genuinely differ.
Does composition make sense for relations that are not functions?
Yes, and the ordered-pairs definition given here generalizes directly: for relations and , define , exactly the formula given in the Definition above but without requiring uniqueness of . 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 may relate a single to multiple elements of even when and 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.