Surjective Functions Suppose we have a function $g:\mathbb{R}\rightarrow\mathbb{R}$ defined
as:
$$
g(x)=x^{2}
$$
Now, we know that this function is not onto because it is not defined
for negative values of $g.$ However, that is because we have defined the mapping from the set of real numbers to the set of real numbers.
If in fact, we had defined it is:
$$
g:\mathbb{R}\rightarrow\mathbb{R}\mbox{+}
$$
the function would have been onto. As such, it seems that our calling
a function onto or not depeneds crucially on how we define the mapping.
For instance, if we defined a function as going from its domain to
its co-domain, it would always be onto. Why do we have to allow
for cases when its not onto? 
 A: Sometimes the codomain is specified for reasons outside of the function you're looking at. You might not know the range of your function, but you know some suitable codomain.
Here is a modest example from linear algebra. Let $A$ be an $m\times n$ matrix, then we can ask whether the equation $Ax=b$ has a solution in $\mathbb{R}^n$ for every $b\in\mathbb{R}^m$. The answer depends on whether the mapping $x\mapsto Ax$ is onto the codomain $\mathbb{R}^m$. The mapping is clearly onto its range, but what we want to know is whether its range includes all $m$-dimensional vectors or not.
A: We don't have to allow for such cases, but it's easier. It's easier to look at real valued functions as all having the same domain, because then it's easier to look at compositions of functions.
If We would always limit functions to their domains, then a function like
$$f(x)=e^{\sin x}$$
would be more difficult to define, since $x\mapsto e^x$ is defined on $\mathbb R$, while the codomain of $x\mapsto \sin x$ is $[-1,1]$.
So, you would then have to say that $f(x)=\exp_{[-1,1]} (\sin x)$, where $\exp_A$ is the exponential function whose domain has been reduced to $A$.
A: I agree with the current answers, and I want to give you another important example.
If you consider the mapping
$$\exp\colon \mathbb C\to \mathbb C,$$
it is not trivial but it is an important theorem that $\exp(\mathbb C)=\mathbb C\setminus \{0\}$.
Once you have proved that you can say that $\exp:\mathbb C\to \mathbb C \setminus\{0\}$ is onto.
It is only one example among many others.
