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.
Consider a function for example $f:R\to R$ defined by $f(x)=x^2$. The domain is the largest possible set of inputs which in this case the set of all real numbers. The codomain is given as $R$, the set of all real numbers. The range is the set of all possible outputs which is the interval $[0,\infty)$.
The image of a subset $A$ of of real numbers is $f(A)$ which is the set of all $f(x)$ where $x\in A$. For example, $f((-1,1))=[0,1)$.
The pre-image of a subset $B$ of the range is the set $f^{-1}(B)$ of all inputs $x$ such that $ f(x)$ is in $B$. For example $f^{-1}([1,4])=[-2,-1]\cup [1,2]$.
Best Answer
You made a mistake here. The range of sine is a closed interval, which we denote with $[-1, 1]$, not an open one $(-1,1)$.
Yup, real numbers. But they are usualy denoted with $\mathbb R$ (LaTeX/MathJax
\mathbb R
), not $\Re$ (\Re
).You're wrong. Redefining the codomain may change properties of a function. Giving the sine function a codomain of $(-2,2)$ doesn't change it much, but giving it $[-1,1]$ changes a lot:
$$\sin : \mathbb R \to [-1,1]$$
is a surjection (a function "onto"), while
$$\sin : \mathbb R \to \mathbb R$$
is not.
Redefining a domain may change the function's properties, too:
$$\sin : \left[0, \tfrac\pi 2\right] \to \mathbb R$$
is an injection (a function "into"), while
$$\sin : \left[0, \pi\right] \to \mathbb R$$
is not.
To answer specifically the last sentence from the question:
Here I copy what I previously added in the comment below:
we need codomains, because we sometimes need to consider functions, whose definition is known together with a codomain, but the range is unknown. Sometimes we do not even have the definition, only some properties are known and we are satisfied with knowing the codomain without narrowing it to the range ("suppose $f$ is a real-valued function such that...; show $f$ is constant" – we know the codomain is $\mathbb R$ and we just need to show the range is one-point, not necessarily which one).
Expansion:
Be also aware that the range of a function may be hard to describe. For continuous real functions we consider at schools the range is often an interval or a sum of intervals – but those are special cases. There are functions with much less regular ranges.
For example see this question at Math.SE: Show that the function f is continuous only at the irrational points for a function described also at Wikipedia: Thomae's function – it is defined on real numbers, but its range is a set of reciprocals of all natural numbers and zero: $$\mathbb R \to \{\tfrac 1n:n\in\mathbb N\}\cup\{0\}.$$ One can easily declare a function whose range is literally any predefined nonempty set $S\subseteq\mathbb R$ – just choose any $s\in S$ and define: $$f:x\mapsto \begin{cases}x&\text{if }x\in S,\\s&\text{otherwise.}\end{cases}$$
In more general approach the range can be even harder to describe analytically.
Consider a function, whose parameter is real and values are pairs of real numbers (or complex numbers, which is equivalent to latter thanks to the complex plane by Jean-Robert Argand). If the function is continuous, its range is a curve on a plane. For example if the function is a position of a projectile in terms of a height and a distance, we get a complete trajectory. It's not too likely one will need to compare such trajectories – we will usually be interested in the maximum height and a maximum distance reachable under some conditions, but not the whole shape. Anyway, it is possible. But how would you compare a curve of a ballistic trajectory to a simple square? ...to a Koch snowflake? ...to the Warsaw Circle? ...or to a Heighway dragon?
And how about non-continuous functions, or those defined on some subsets of $\mathbb R$, whose ranges may become any figure on the plane, for example a family of concentric circles intersected by a family of parallel lines? ...or the interior of an annulus?
Things get even more weird if the 'target space' of a function is some more complex set, like a space of integer sequences, a space of real matrices $5\times 5$, a space of real functions integrable over a unit interval, and so on. You don't always need to know the range of a function, often it's just enough to know what its codomain is.