I'm a little confused between the difference between the range & co-domain of a function. Are they not the same thing (i.e. all possible outputs of the function)?
Functions – Domain, Co-Domain & Range of a Function
functionsterminology
Related Solutions
What is the point of having values in the codomain that can not be output by the function, how does that aid in describing the function?
Here are a few reasons why we allow some functions to not be surjective.
As Lubin mentioned, the range of a function can be difficult to determine. For example, determining the range of a polynomial of high even degree (such as $P(x) = x^6 - 3x^2 + 6x$) amounts to finding the zeroes of a high-degree polynomial (such as $P'(x) = 6x^5 - 6x + 6$, whose roots are not expressible as radicals), a difficult task in general. We could get around this by defining the codomain of every function $f$ to be $\operatorname{im} f$ (that is, $\{y\,:\,f(x)=y\text{ for some }x\in X \}$), but that doesn't really add any information.
It's nice to separate surjective functions from other functions because surjective functions are dual to injective functions. When I say "dual" I'm referring to, for example, the following fact: a function $f:A\to B$ is injective if and only if there is a function $g:B\to A$ such that $g\circ f=1_A$ (by $1_A$ I mean the identity function on $A$); a function $f:A\to B$ is surjective if and only if there is a function $g:B\to A$ such that $f\circ g=1_B$. When you study the branch of mathematics known as category theory, you'll see that it's very natural to have dual properties like this.
Does this also mean that the domain can include numbers that the are not inputs to the function?
As others have remarked, the domain of a function can include other objects than numbers. For example, you could define a function which takes as input a person and returns his age. In any case, a function must be defined on all possible input values. The answer to your second question is no.
And is it also then true that is a function is "onto" the codomain is the same as the image? So surely any function can be "onto" if you just change the what the codomain is?
That's exactly right. You can make any function onto by changing the codomain. But as I remarked earlier, in general we don't know what the image of a function is and so it doesn't add any information to restrict the codomain.
What I'm really trying to ask I guess is the range/image of a function is defined by the function, what defines the codomain?
The codomain usually arises naturally in the definition of the function. For example, whenever you have a function which returns a number, the natural choice of codomain is $\mathbb R$. Of course, if by "number" you mean "complex number" then the codomain could be $\mathbb C$; if by "number" you mean "quaternion" then the codomain could be $\mathbb H$.
On the other hand, owing to the set-theoretic fact that "there is no set containing everything," it's not possible to pick a single universal codomain for functions.
When I wrote up this answer I realized that I used to ask the same questions as you, but I stopped once I had learned enough mathematics. I can't give you a single profound reason why we don't make all functions surjective besides a pragmatic one: surjectivity is a useful notion, and getting rid of it would be unprofitable.
The requirement here is that we need $g \circ f$ to make sense and that is possible when $f(C) \subseteq \text{domain}\ (g) = B$. So if $C = \text{range}\ (f) \cap A \cap B,$ then $x \in C \Rightarrow x \in A \Rightarrow f(x) \in \text{range}\ (f)$. This is the best we can conclude from here. So there may very well be some elements in range$\ (f)$ such that they are not in domain $(g)$. Hence we can not define $g \circ f$ properly from this.
Consider for example: $f : \mathbb{R} /\{0\} \longrightarrow \mathbb{R}$ defined as $f(x) = \frac{1}{x}$ and $g: \mathbb{R}/\{1,2\} \longrightarrow \mathbb{R}, g(x) = \frac{1}{x^2-3x+2}$. Then, we can see that $g \circ f: \mathbb{R}/\{\frac{1}{2},1\}\longrightarrow \mathbb{R},g(f(x)) = \frac{x^2}{1-3x+2x^2} = \frac{x^2}{(2x-1)(x-1)} $. For the moment we just focus on the domains.
We can here see that $C = \mathbb{R}/\{1, \frac{1}{2}\} \ne \text{range}\ (f) \cap A \cap B$ as $\frac{1}{2} \in \text{range}\ (f)\cap A\cap B $ but doesn't in $C$.
We can construct many examples like this. The key is to understand when is $g \circ f$ defined. Hope this helps.
Best Answer
I think your confusion may stem from differing uses of terminology.
For a function $f: X \rightarrow Y$, the codomain is just the set $Y$. For instance, if $f: \mathbb{R} \rightarrow \mathbb{R}$ is $x \mapsto x^2$, then the codomain is $\mathbb{R}$. This terminology is agreed upon by all who use it: i.e., I have never seen anyone use the term "codomain" to mean anything else.
Unfortunately the term range is ambiguous. It is sometimes used exactly as codomain is used above, so some say that $\mathbb{R}$ is the range of the squaring function defined above. However, those who use the term codomain at all usually reserve the term "range" for the subset $\{y \in Y \ | \exists x \in X \text{ such that } f(x) = y\}$, i.e., the subset of values which are actually mapped to by some element in the domain. (Some others use the term image for this instead.) So in the above example the image of the function is $[0,\infty)$. Whether the range is $\mathbb{R}$ (i.e., the codomain) or $[0,\infty)$ (i.e., the image) depends upon your convention, and both are rather prevalent.
In practice, this means that it would be safest never to use the term range, instead using codomain and image. (But most people don't do that either...)