First we show that for each element of the domain there exists an element in the codomain to which it is mapped. Let
\(x \in X\text{.}\) Because
\(g\) is a function there exists a
\(y \in Y\) such that
\(g(x)=y\text{.}\) Because
\(f\) is a function, there exists a
\(z \in Z\) such that
\(f(y)=z\text{.}\) This means for all
\(x \in X\) there exists a
\(z \in Z\) such that
\(h(x)=z\text{.}\)
Second we show that each element of the domain is mapped to a unique element of the codomain. Suppose for sake of contadiction that there exists
\(x \in X\) such that
\(h(x)=z_1\) and
\(h(x)=z_2\text{.}\) This means that
\(f(g(x))=z_1\) and
\(f(g(x))=z_2\text{.}\) Because
\(g\) is a function there exists a unique
\(y \in Y\) such that
\(g(x)=y\text{.}\) Thus
\(f(y)=z_1\) and
\(f(y)=z_2\text{.}\) But this contradicts that
\(f\) is a function which maps an element of the domain to a unique element of the codomain.
Thus the composition of two functions is a function.