Is it necessary for the codomain of the inner function to be equal to the domain of the outer function in function composition?

Recently I came across a statement which meant roughly the following.

If we want to apply composition of functions, then the co-domain of the inner function should be equal to the domain of the outer function. Mathematically, if $f$ and $g$ are functions and $f : A \rightarrow B$ and $g: C\rightarrow D$, so that we can perform $fog$ we must have $D=A$ and the pictorially the situation is something like this: enter image description here

But I do not think it is necessary to have $A=D$ in the above diagram. I mean even if $D \subseteq A$ then I guess our composition shall work just fine. But in that case one might argue that the co-domain of the inner function $g$ degenerates to $A$. Yes it is fine, but if strictly $g$ is defined as $g: C\rightarrow D$ and $D\subseteq A$ then we can define a new function $h$ which is same as $g$ except that the co-domain is degenerated to $A$. But $h$ is a new function after all different from $g$ as far as the domain and co-domain is concerned.

For example if we have $g: \mathbb{N} \rightarrow \mathbb{N}$ and $g(x)=x$ and $f:\mathbb{R} \rightarrow \mathbb{R}$ and $f(x)=\sin (x)$ then without any problem we can do $h(x)=f(g(x))=\sin (x)$ where $h: \mathbb{N} \rightarrow \mathbb{R}$

Below is an excerpt from the text Thomas' Calculus:

enter image description here

Which I guess has intuitively more generalized the situation (and more practical though) that range of $g$ should be subset of domain of $f$.


You are right in the sense that codomains tend to be overly specific. We specify both a domain and codomain anyway to give context to our discussion of a function. If you want to narrow that context, by all means restrict the domain or codomain.

You might be interested in the composition of relations. A relation $r:A \to B$ is a subset $r \subseteq A \times B$. Like functions, relations don't have to implicate every member of their codomain (there may be $b \in B$ that don't appear as the second coordinate of a member of $r$). But unlike functions, relations don't need to implicate every member of their domain either (there may be $a \in A$ that don't appear as the first coordinate of a member of $r$). So both the domain and codomain of relations may be overly specific.

We can compose relations $r : A \to B$ and $s: B \to C$. The composite $s \circ r : A \to C$, which I like to read as "$s$ after $r$", is defined by $$ s \circ r = \{(a,c) \in A \times C \;:\; \exists b\in B \text{ such that } (a,b) \in r \text{ and } (b,c) \in s \}. $$ Note that this definition of composition doesn't care so much about every member of $A,B,C$, only those elements implicated by one of the relations $r,s$. Also note that the composite $s \circ r$ may implicate less members of $A$ then did $r$. That is, there may be $a \in A$ that appear as the first coordinate of a unique pair $(a,x) \in r$, but with no $c \in C$ for which $(x,c) \in s$, so that $a$ was lost in composing $r$ with $s$.


The source of this confusion is the confusion between the concepts of the codomain and of the range of a function (or a relation, which the earlier answer described so well). Unfortunately, too many textbooks gloss over this distinction…

Strictly speaking, it is true by the set-theoretic definition that for a composition $f\circ g$ of functions $f: A\to B$ and $g: C\to D$ to be well-defined we have to have $D=A$, i.e. that the codomain of the inner function is equal to the domain of the outer function. So the first definition is correct, period.

But… What you're saying is also true, as long as we make it clear that you're talking about the range of the inner function $g$, which can be smaller than its codomain if $g$ is not surjective (i.e., not onto).

For example if we have $g:\mathbb{N}\to\mathbb{N}$ and $g(x)=x$ and $f:\mathbb{R}\to\mathbb{R}$ and $f(x)=\sin(x)$ then without any problem we can do $h(x)=f(g(x))=\sin(x)$ where $h:\mathbb{N}\to\mathbb{R}$.

No, technically speaking we can't. But what we can do is consider $g$ as a function $\mathbb{N}\to\mathbb{R}$, i.e. with codomain $\mathbb{R}$. Then the definition $g(x)=x$ for all $x\in\mathbb{N}$ still gives us a well-defined function, whose range just happens to be $\mathbb{N}\subseteq\mathbb{R}$. So we don't lose anything, but now we can compose it with $f$ the way you described.

I can see how there's a tension from the pedagogical perspective on what's best when teaching compositions to students at an elementary level. Going into strict set-theoretic definition at that point would probably be (imho) a pedagogical disaster. That's why, when composition of functions appears in school algebra textbooks, the more practical albeit non-rigorous approach of describing it with "the range of $g$ must be inside the domain of $f$" is good enough. And that's precisely what you see in this screenshot from Thomas's Calculus (where it's borrowed from more elementary textbook for its review chapter, since this is not Calculus content per se yet).

But once we mature and start talking at a more appropriate language, we need to clearly distinguish codomain and range from each other, and we need to get our definitions right too.