Suppose you have two functions $f:X\to Y$ and $g:Y\to Z$:

$$X\stackrel{f}{\to}Y\stackrel{g}{\to}Z$$ |

Then you can make a new function $X\to Z$ whose rule is “do $f$, then do $g$”.

Let $f:X\to Y$ and $g:Y\to Z$. The composition of $g$ and $f$, written $g\circ f$ or $gf$, is the function $X\to Z$ with rule $(g\circ f)(x)=g(f(x))$.

This makes sense because $f(x)$ is an element of $Y$ and $g$ has domain $Y$ so we can use any element of $Y$ as an input to $g$.

It’s important to remember that $g\circ f$ is the function whose rule is “do $f$, then do $g$”.

If $f\mathrm{:}X\mathrm{\to}Y$ then $f\mathrm{\circ}{\mathrm{id}}_{X}\mathrm{=}{\mathrm{id}}_{Y}\mathrm{\circ}f\mathrm{=}f$.

For any $x\in X$ we have $(f\circ {\mathrm{id}}_{X})(x)=f({\mathrm{id}}_{X}(x))=f(x)$ and $({\mathrm{id}}_{Y}\circ f)(x)={\mathrm{id}}_{Y}(f(x))=f(x)$. ∎

Functions $f$ and $g$ such that the codomain of $f$ equals the domain of $g$, in other words, functions such that $g\circ f$ makes sense, are called composable. Suppose that $f$ and $g$ are composable and $g$ and $h$ are also composable, so that we can draw a diagram

$$X\stackrel{f}{\to}Y\stackrel{g}{\to}Z\stackrel{h}{\to}W.$$ |

It seems there are two different ways to compose these three functions: you could first compose $f$ and $g$, then compose the result with $h$, or you could compose $g$ with $h$ and then compose the result with $f$. But they both give the same result, because function composition is associative.

Let $f\mathrm{:}X\mathrm{\to}Y\mathrm{,}g\mathrm{:}Y\mathrm{\to}Z\mathrm{,}h\mathrm{:}Z\mathrm{\to}W$. Then $h\mathrm{\circ}\mathrm{(}g\mathrm{\circ}f\mathrm{)}\mathrm{=}\mathrm{(}h\mathrm{\circ}g\mathrm{)}\mathrm{\circ}f$.

Both $h\circ (g\circ f)$ and $(h\circ g)\circ f$ have the same domain $X$, same codomain $W$, and same rule that sends $x$ to $h(g(f(x)))$. ∎

The associativity property says that a composition like $h\circ g\circ f$ doesn’t need any brackets to make it unambiguous: however you bracket it, the result is the same. In fact we can omit brackets from a composition of any length without ambiguity.