In a lot of cases, the domain and codomain change the function itself.
The domain is clearly important, since a change of the domain changes what you're allowed to "plug into" the function.
For example, if I have a function $f(x) = 2x$, it's not a function yet since I need to define the domain first. For example, if the domain is:
- $\{0\}$ then the function is the trivial function that takes $0 \to 0$.
- $\{0, 1\}$, then the function is one that maps $0 \to 1$, $1 \to 2$
- $\mathbb{N}$, Then the function maps all natural numbers by doubling them
- $\mathbb{Z}/3\mathbb{Z}$, that is, the set of integers modulo $3$. In this case, it takes the numbers $\{0, 3, 6, \ldots \} \to 0$, $ \{1, 4, 7, 10, \ldots \} \to 2$, $\{2, 5, 8, \ldots\} \to 1$.
The codomain is trickier to motivate, but it lets us differentiate between "where the function is allowed to hit" and "where the function does hit"
The image is the set of all points $\{y \ | \ y = f(x) \ \forall x \in domain \}$, which is the set of all points the function hits when coming from the domain.
However, the codomain is what is supplied by the creator of the function. Hence, the codomain need not equal the image (it must be at least the image, but it can be larger)
For example, if the function is $f(x) = 2x$, $f: \mathbb{N} \to \mathbb{N}$, then this function has a larger codomain than range. This is because a value such as $3 \in \mathbb{N}$, there is no $x \in \mathbb{N}$ such that $f(x) = 3$
However, if I had said $f(x) = 2x$, $f : \mathbb{N} \to {0, 2, 4, \ldots}$, then the function would have image = codomain.
Functions that access their entire codomain are called as "onto" or "surjective" (from french, $sur$ meaning "on") since they in some sense map "on" the entire codomain rather than just in the image.