Codomain of a function At high school we were told that a function has a domain and a range, the function maps from the domain to the range. Such that the domain contains all and only the possible inputs and the range contains all and only the possible outputs.
Now at University I'm told a function has a domain and a codomain, and that the codomain contains all the possible outputs but may also include other numbers. 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? Does this also mean that the domain can include numbers that the are not inputs to the function?
Surely this means you could say the codomain of any function (that outputs numbers) is the complex set (so all numbers)? 
EDIT: Wikipedia says the function $f : x \rightarrow x^2$ has codomain $\mathbb{R}$ but it's image (what I guess I knew as range in high school) is $\mathbb{R}^+_0$, so why not just say the codomain is $\mathbb{R}^+_0$.
EDIT2: 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?
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?
 A: First of all, not everything in mathematics is numbers. There are other objects, all of which are legitimate values for a function. Here is a simple example:
$f(x,n)=x^n$ can actually be seen as a function, $F(n)$ returns the function which maps $x$ to $x^n$. So $F$ takes in a natural number, and outputs a function from the real numbers to the real numbers.
Now we consider these for different $n$'s, when $n=0$ then $F(n)$ is the constant function $1$. When $n=1$ the range is $\Bbb R$. When $n=2$ the range is $\Bbb R^{\geq 0}$. And so on and so forth.
Each of these values gives us a different range. But if we think of all of them as functions from $\Bbb R$ to $\Bbb R$ then we can "meld" them together into one big function $f(x,n)=x^n$.
To some extent, you are right. The invention of "codomain" is a bit artificial. In parts of mathematics, a function comes with an associated codomain (and thus changing the codomain means changing the function), and in other parts of mathematics, the codomain is an external property assigned to the function and can be changed whenever we want to (as long as it includes the range of the function).
So this is a matter of how you intend to use "functions" as mathematical objects. Of course, a lot of the examples for when a codomain matters come from a slightly later stage. If you study mechanical engineering then it's possible that you'll never even have to worry about this; and if you study mathematics, then it's likely that you've already run into examples (without knowing that these are examples, perhaps).
A: 
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.
A: The codomain is a set which the function maps into. For example if $f:N \rightarrow R$ by $f(n) = n$ then R is the codomain.The range of the function is the subset of the codomain whose elements correspond to the mapping of some element from the domain. So with $f(n) = n$ the range in $R$ is the subset $N \subset R$.
If the range is equal to the codomain, then the function is called onto, or a surjection..
A: To understand this issue, it is worth noting that there are a couple of ways you can specify a function.  At a minimum, you need to specify a domain $\mathcal{X}$ and a graph $\mathscr{G} \equiv \{ (x, f(x)) |x \in \mathcal{X} \}$ which is the set of all pairs of values $(x,f(x))$.  The function cannot take in any values that are not in its domain, but the domain need not be a set of "numbers".  The range of the function then usually refers to the set of possible output values, which is defined by $\text{Range} f \equiv \{ f(x) | x \in \mathcal{X} \}$.  This version of a function does not have a codomain, and so it makes no sense to ask whether or not it is "surjective".
There is a broader type of function where you also specify a codomain.  The version that includes a codomain is a triple $f = (\mathcal{X}, \mathcal{Y}, \mathscr{G})$ that includes a domain $\mathcal{X}$, a codomain $\mathcal{Y}$ and a graph $\mathscr{G}$.  In this latter version of a function, you have both a range and a codomain.  The latter has to be specified as an additional part of the function, since it is not uniquely determined from the domain and graph of the function (so you are right to wonder what "defines" the codomain).  As you are no doubt aware, the codomain always contains the range, but they are not always equivalent.  If the range is the codomain then we say that the function is "surjective".  If a function has a codomain that is larger than the range, then as you point out, there are elements in the codomain that cannot be produced by the function.
You are correct when you assert that any function can be turned into a surjective function by reducing its codomain down to its range.  Doing this is called an "induced surjection" --- i.e., the induced surjection of the function $f = (\mathcal{X}, \mathcal{Y}, \mathscr{G})$ is the function $f_* = (\mathcal{X}, \text{Range}(f), \mathscr{G})$.  (I have used different notation for the two, but one sometimes plays a bit fast-and-loose with notation here.)  Similarly, any surjective function with a specified codomain (equal to its range) can have its codomain enlarged so that it is no longer surjective.

So, why bother specifying a codomain at all? Although it is possible to work with functions without specifying a codomain (see the first form of function above), there are various reasons why it is often useful to include specification of a codomain in a function.

*

*As pointed out by Lubin, for many functions, the range is difficult to compute.  In such cases it is usually extremely easy to specify a codomain that is known to contain the range, but it is hard to determine the range.  This has the benefit that it at least "narrows down" the range to being a subset of a specified set.


*As pointed out by user134824, there are contexts where it is useful to be able to separate "surjective" mappings from non-surjective mappings, and to do this we require specification of a codomain (in order for the concept of surjectivity to make sense).  He gives the example of relating injective and surjective functions as dual functions, but there are also other contexts where you might want to have both a surjective and non-surjective version.


*There are many contexts where we are not just interested in an individual function, but rather, we are interested in classes of functions defined for a particular domain and codomain.  For example, when you learn combinatorial theory, various counting formulae can be framed by counting equivalence classes of functions between a finite domain and finite codomain, and these are then formulated into the famous "twelvefold way".  In this classification, we count the size of the classes of functions that map between a particular domain and codomain, and this leads to a number of useful formulae for counting problems.  If you have a look at the linked table showing the twelvefold way, you will see that only one column (four of the ways) restrict attention to surjective functions.
A: As many of the commenters have pointed out, it is oftentimes useful to have a set of objects in mind that the function potentially maps to, even though the function does not map to all of those obejcts, i.e. The function is not surjective (or, what is the same thing, onto).
But what I don't understand, is that why we don't typically do the same thing on the input side: for many of the same reasons that I can see (e.g it may be hard to determine what inputs the functions is actually defined for), I would think it would be just as useful to have a set of objects in mind that the function potentially maps from, even though the function does not map all of those objects. Now, as it so happens, mathematicians do have terminology for this: a function that is not defined for all these potential inputs is known as a partial function, while a function that is defined for all of its potential input values is knows as a total function. But, typically, functions are supposed to be total ... Indeed, according to many texts, a partial function is by definition simply impossible.  Again, I don't understand this practice. It is not symmetrical from how we treat things on the output side, and like I said, I would think many of the reasons for having a 'co-domain' that can be larger than the actual 'range' of a function can be applied on the input side as well.
In fact, I would think that whenever we define a function, one would have to indicate such a potential set of inputs in the first place. For example, if I define a function $f(x) = x^2$, then shouldn't I have to indicate what set the $x$ values come from? Is it the natural number, the real number, the complex numbers, ... or what? Now, we have a handy dandy notation of the form $f:A \rightarrow B$ ... So why not use that notation to define the potential input and output values?  But no, no such luck: $A$ is supposed to be the domain, by which it is understood the set of values for which the function is actually defined, not potentially. Indeed, a function like $f(x) = 1/x$ is supposed to written $f:R/{0} \rightarrow R$ (or is it $f:R/{0} \rightarrow R/{0}$?!  ... All assuming  I intended this to be about the real numbers in thefirst place ...), all because functions have to be total. 
Again, I find all of this highly unfortunate. I would say: the function $f:R \rightarrow R$ as defined by $f(x) = 1/x$ is a partial surjective function whose domain and codomain of discourse is $R$ and whose domain and codomain of definition is $R/{0}$. Wouldn't that be a lot easier and symmetrical? In fact, I bet if we were to follow  this convention, far fewer high school student would be confused about 'domain', 'codomain' and 'range'.
And one more thing: I assume 'co-domain' is short for 'converse domain' and as Russell and Whitehead defined it, the converse domain is the domain of the converse (better known as inverse) of a function or relation (assuming the function is injective of course). OK, that is all nicely logically defined (as one would expect from a book called Principia Mathematica!) but now look at what a mess we have made of this in the modern usage of 'domain' and 'co-domain': the function $f(x)=1/x$ has as domain $R/{0}$, and as co-domain $R$. But the domain of its inverse/converse is $R/{0}$ (and don't ask me what its co-domain is supposed to be...) That is, the 'co-domain' is not the domain of its converse! Russell and Whitehead must be rolling in their graves!
