# Function composition

Function composition is an operation on functions that, given two functions \( f \colon X \to Y \) and \( g \colon Y \to Z \), produces a new function \( g \circ f \colon X \to Z \) defined by \( (g \circ f)(x) = g(f(x)) \): the output of \( f \) becomes the input of \( g \).<sup>[1](https://www.homepages.ucl.ac.uk/~ucahmto/0005_2021/Ch2.S7.html)</sup> The operation is associative but not commutative, and it underlies the definition of a category in mathematics.<sup>[2](https://courses.maths.ox.ac.uk/mod/page/view.php?id=65663)</sup>

| Key fact | Detail |
|---|---|
| Definition | For \( f \colon X \to Y \) and \( g \colon Y \to Z \), \( (g \circ f)(x) = g(f(x)) \); the notation is read "g composed with f"<sup>[1](https://www.homepages.ucl.ac.uk/~ucahmto/0005_2021/Ch2.S7.html)</sup> |
| Composability | \( g \circ f \) is defined only when the codomain of \( f \) equals the domain of \( g \)<sup>[1](https://www.homepages.ucl.ac.uk/~ucahmto/0005_2021/Ch2.S7.html)</sup> |
| Associativity | \( h \circ (g \circ f) = (h \circ g) \circ f \) whenever all compositions are defined<sup>[2](https://courses.maths.ox.ac.uk/mod/page/view.php?id=65663)</sup> |
| Commutativity | In general \( f \circ g \neq g \circ f \); for \( f(x) = x^2 \) and \( g(x) = e^x \), \( (f \circ g)(x) = e^{2x} \neq e^{x^2} = (g \circ f)(x) \)<sup>[2](https://courses.maths.ox.ac.uk/mod/page/view.php?id=65663)</sup> |
| Identity | \( f \circ \mathrm{id}_X = \mathrm{id}_Y \circ f = f \)<sup>[1](https://www.homepages.ucl.ac.uk/~ucahmto/0005_2021/Ch2.S7.html)</sup> |
| Inverses | If \( f \) and \( g \) are invertible, \( (g \circ f)^{-1} = f^{-1} \circ g^{-1} \)<sup>[2](https://courses.maths.ox.ac.uk/mod/page/view.php?id=65663)</sup> |
| Continuity | If \( g \) is continuous at \( x_0 \) and \( f \) is continuous at \( g(x_0) \), then \( f \circ g \) is continuous at \( x_0 \)<sup>[3](https://mathworld.wolfram.com/FunctionComposition.html)</sup> |

## Definition and notation

Given \( f \colon X \to Y \) and \( g \colon Y \to Z \), the composition \( g \circ f \) is the function from \( X \) to \( Z \) whose rule is "do \( f \), then do \( g \)". The definition makes sense because \( f(x) \) is an element of \( Y \), and \( g \) has domain \( Y \), so \( f(x) \) can be used as an input to \( g \).<sup>[1](https://www.homepages.ucl.ac.uk/~ucahmto/0005_2021/Ch2.S7.html)</sup> The order of the notation matters: \( g \circ f \) means \( f \) is applied first, even though \( g \) is written on the left.<sup>[1](https://www.homepages.ucl.ac.uk/~ucahmto/0005_2021/Ch2.S7.html)</sup>

Two functions are called composable when the codomain of the first equals the domain of the second, so that their composition makes sense.<sup>[1](https://www.homepages.ucl.ac.uk/~ucahmto/0005_2021/Ch2.S7.html)</sup> In elementary treatments the condition is often stated in terms of domains: the domain of the composite \( f \circ g \) consists of all \( x \) in the domain of \( g \) for which \( g(x) \) lies in the domain of \( f \).<sup>[4](https://openstax.org/books/college-algebra-2e/pages/3-4-composition-of-functions)</sup>

A function \( h(x) = f(g(x)) \) built this way is sometimes called a composite function.<sup>[3](https://mathworld.wolfram.com/FunctionComposition.html)</sup> Composition is a binary operation on functions, forming a new function much as addition forms a new number, but it should not be confused with pointwise multiplication: in general \( f(g(x)) \neq f(x) \cdot g(x) \).<sup>[4](https://openstax.org/books/college-algebra-2e/pages/3-4-composition-of-functions)</sup>

## Algebraic properties

**Associativity.** For three composable functions \( f \colon X \to Y \), \( g \colon Y \to Z \), and \( h \colon Z \to W \),

\[ f \circ (g \circ h) = (f \circ g) \circ h. \]

The proof is a direct computation: for \( x \in X \), both \( (h \circ g) \circ f \) and \( h \circ (g \circ f) \) evaluate to \( f(g(h(x))) \).<sup>[2](https://courses.maths.ox.ac.uk/mod/page/view.php?id=65663)</sup><sup> • </sup><sup>[13](https://math.libretexts.org/Bookshelves/Combinatorics_and_Discrete_Mathematics/Applied_Discrete_Structures_%28Doerr_and_Levasseur%29/07%3A_Functions/7.03%3A_Function_Composition)</sup> As a consequence, a chain such as \( h \circ g \circ f \) needs no brackets, because every bracketing gives the same function.<sup>[1](https://www.homepages.ucl.ac.uk/~ucahmto/0005_2021/Ch2.S7.html)</sup>

**Non-commutativity.** Composition is generally not commutative. If \( X \neq Y \), the two composites \( f \circ g \) and \( g \circ f \) may have different domains and codomains; even when \( X = Y \) they usually differ. For \( f(x) = x^2 \) and \( g(x) = e^x \), both maps from \( \mathbb{R} \) to \( \mathbb{R} \),

\[ (f \circ g)(x) = e^{2x} \neq e^{x^2} = (g \circ f)(x). \]

<sup>[2](https://courses.maths.ox.ac.uk/mod/page/view.php?id=65663)</sup>

**Identity.** Composing any function with the identity function on the appropriate set returns the function unchanged: \( f \circ \mathrm{id}_X = \mathrm{id}_Y \circ f = f \).<sup>[1](https://www.homepages.ucl.ac.uk/~ucahmto/0005_2021/Ch2.S7.html)</sup> If every function being studied sends one fixed set \( X \) back to itself, then composing them gives a binary operation on the collection of all such self-maps of \( X \), an operation that is associative and has an identity.<sup>[5](https://watanabe.you/wiki/Function%20Composition)</sup>

**Injectivity, surjectivity, and inverses.** Composition preserves injectivity: if \( f \) and \( g \) are injective, so is \( g \circ f \). Conversely, if \( g \circ f \) is injective then \( f \) is injective, but \( g \) need not be.<sup>[2](https://courses.maths.ox.ac.uk/mod/page/view.php?id=65663)</sup> If \( f \) and \( g \) are invertible, their composite is invertible and the inverse is the reversed composition of the inverses,

\[ (g \circ f)^{-1} = f^{-1} \circ g^{-1}. \]

<sup>[2](https://courses.maths.ox.ac.uk/mod/page/view.php?id=65663)</sup> For an invertible \( f \colon A \to B \) with inverse \( f^{-1} \colon B \to A \), the two composites \( f^{-1} \circ f \) and \( f \circ f^{-1} \) are both identity functions, but on different sets: \( \mathrm{id}_A \) and \( \mathrm{id}_B \) respectively.<sup>[6](https://www.csd.uwo.ca/~abrandt5/teaching/DiscreteStructures/Chapter3/functions.html)</sup>

## Analysis and calculus

Composition interacts with limits and continuity. If \( g \) is continuous at \( x_0 \) and \( f \) is continuous at \( g(x_0) \), then \( f \circ g \) is continuous at \( x_0 \).<sup>[3](https://mathworld.wolfram.com/FunctionComposition.html)</sup> Differentiation of a composite is governed by the chain rule, and Faà di Bruno's formula gives an explicit expression for the \( n \)-th derivative of \( f(g(t)) \).<sup>[3](https://mathworld.wolfram.com/FunctionComposition.html)</sup>

## Role in category theory

[Category theory](https://www.edgepedia.org/category-theory) abstracts composition itself. A category consists of objects and morphisms together with a law of composition \( (g, f) \mapsto g \circ f \) that is associative and has an identity morphism for each object.<sup>[7](https://mathweb.tifr.res.in/~sandeepv/lecture1_web.html)</sup> Sets and functions form the canonical example, the category **Set**, in which composition is ordinary function composition and the identity morphism on a set is its identity function.<sup>[7](https://mathweb.tifr.res.in/~sandeepv/lecture1_web.html)</sup> The same two laws, associativity and the identity laws, are exactly what the definition of a category requires, so any collection of functions closed under composition and containing the relevant identity maps forms a concrete category.<sup>[8](https://www.math.purdue.edu/~gottlieb/Courses/atopol.pdf)</sup>

## In software

Several computer algebra and programming systems provide composition as a built-in operation. The [Wolfram Language](https://www.edgepedia.org/wolfram-language) implements it as `Composition[f1, f2, ...]`, which composes on the left; `Composition[]` gives the identity function, and `InverseFunction[Composition[f, g, h]]` produces the reversed composition of inverses.<sup>[9](https://reference.wolfram.com/language/ref/Composition)</sup> MATLAB's Symbolic Math Toolbox provides `compose(f,g)`, which returns \( f(g(y)) \).<sup>[10](https://www.mathworks.com/help/symbolic/sym.compose.html)</sup> In Haskell, the `.` operator performs right-to-left function composition, and a left-to-right variant `>>>` is available in the `Control.Category` module.<sup>[11](https://downloads.haskell.org/ghc/9.10.0.20240413/docs/libraries/base-4.20.0.0-0f99/Prelude.html)</sup> The APL language generalizes the mathematical notion, using composition operators to glue two functions together in several application patterns beyond the simple nesting of one monadic function in another.<sup>[12](https://docs.dyalog.com/20.0/language-reference-guide/primitive-operators/function-composition/)</sup>

## References

1. [Function composition, MATH0005 Algebra 1, University College London](https://www.homepages.ucl.ac.uk/~ucahmto/0005_2021/Ch2.S7.html)
2. [Introduction to University Mathematics (2025-26): Functions, Mathematical Institute, University of Oxford](https://courses.maths.ox.ac.uk/mod/page/view.php?id=65663)
3. [Function Composition, Wolfram MathWorld](https://mathworld.wolfram.com/FunctionComposition.html)
4. [3.4 Composition of Functions, College Algebra 2e, OpenStax](https://openstax.org/books/college-algebra-2e/pages/3-4-composition-of-functions)
5. [Function Composition, Watanabe wiki](https://watanabe.you/wiki/Function%20Composition)
6. [3.1 Functions, Discrete Structures for Computing, Western University](https://www.csd.uwo.ca/~abrandt5/teaching/DiscreteStructures/Chapter3/functions.html)
7. [Lecture 1: Categories and functors, Tata Institute of Fundamental Research](https://mathweb.tifr.res.in/~sandeepv/lecture1_web.html)
8. [Atopology lecture notes, Purdue University](https://www.math.purdue.edu/~gottlieb/Courses/atopol.pdf)
9. [Composition, Wolfram Language Documentation](https://reference.wolfram.com/language/ref/Composition)
10. [compose: Functional composition, MATLAB Symbolic Math Toolbox](https://www.mathworks.com/help/symbolic/sym.compose.html)
11. [Prelude, GHC Base library documentation](https://downloads.haskell.org/ghc/9.10.0.20240413/docs/libraries/base-4.20.0.0-0f99/Prelude.html)
12. [Function Composition, Dyalog APL Documentation](https://docs.dyalog.com/20.0/language-reference-guide/primitive-operators/function-composition/)
13. [7.3: Function Composition](https://math.libretexts.org/Bookshelves/Combinatorics_and_Discrete_Mathematics/Applied_Discrete_Structures_%28Doerr_and_Levasseur%29/07%3A_Functions/7.03%3A_Function_Composition)

---
*Topic: Encyclopedia › Physical world and mathematics › Mathematics and statistics › Logic and discrete mathematics › General discrete mathematics and discrete structures › Discrete mathematics*

*Initially written Sep 17, 2026 · Reviewed: Sep 30, 2026; Oct 15, 2026 · Edited: Sep 30, 2026; Oct 15, 2026 · Last review: Oct 15, 2026*

*Copyright 2026 EdgeChat AI, a subsidiary of Biostate AI.*

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license
