Why is it important to have a discrepancy between image and codomain? A function $f:\mathbb{R}\rightarrow \mathbb{R}$ given by $f(x)=x^2$ has $\mathbb{R}_{\geq0}$ as its image and $\mathbb{R}$ as its codomain.
What's the need for this discrepancy? Why don't we just write $f:\mathbb{R}\rightarrow \mathbb{R}_{\geq0}$? 
I believe the reason for this is that there may be some functions in which stating the image this way may be impractical - if this hypothesis is true, what is this class of functions? If yes or no, is there any other reason for this?  
 A: You cannot speak about surjectivity (that is, whether it is onto) otherwise. If we would define the codomain of every function to be its image, then every function would be surjective.
A: What is a function? Informally, it is a process, or an assignment, from an input set to an output set. It is not just the process or assignment that forms a function, but specifying the input and output is part of what it is. Formally, a function is a triple $(A,B,f)$ where $A,B$ are sets and $f\subseteq A\times B$ is a subset of the cartesian product, i.e., a relation from $A$ $to$ $B$. If instead one wanted to view the function $(A,B,f)$ as equivalent to $(A,Im(f),f)$ then one needs to add a condition to an otherwise very clean definition. So, to force the codomain to be the range actually burdens the definition rather than simplify things. That is one reason not to force such things. 
Another reason is so that one has no problems talking about composition when it's obvious one should be able to talk about composition (so actually this is a category theoretic reasoning). Suppose that $f:A\to B$ and $g:B\to C$. If I want to know if the composition $g\circ f$ exists I don't care what the actual range of the functions is. Just a glance as the domain of $g$ and the (ta ta ta taaaaaa) codomain of $f$. In other words, if the input type of $g$ matches the output type of $f$, then the composition is defined. 
If we insisted that codomain=range, then the condition above will have to be replaced by "the range of $f$ is contained in the domain of $g$". And now I'll get into a bit of technical lingo from category theory. The resulting category from forcing codomain=range will be the category $Set_{Surj}$ of sets and surjections. It's a perfectly legit category, but it has far fewer nice properties when compared to $Set$, the category of sets and all functions. For instance, the empty set is characterized in $Set$ as an initial object (i.e., there is precisely one function from it to any other given set) and is dual to singletons, which are terminal. This is no longer the case in $Set_{Surj}$. The disjoint union of two (or more) sets is an example of a categorical product in $Set$, and is dual to the cartesian product. Disjoint unions are no longer coproducts in $Set_{Surj}$. Many other of the nice properties of $Set$ are lost if passing to $Set_{Surj}$. 
In particular, many very convenient injections will no longer be allowed if we require all functions to be surjective. For instance, it is very convenient to be able to speak of the functions $f_y:\mathbb R \to \mathbb R^2$, given by $f(x)=(x,y)$, or various curves in the plane being parametrized by some function $\gamma:[a,b]\to \mathbb R^2$. 
A: The most obvious reason is just that it's impractical.  The image may be difficult to write down.  Say for example we consider $f(x) = \frac{x^3}{1+e^x}$.
In order to define the image I have to work out the maximum, which involves solving $f'(x) = 0$. But I can't differentiate $f(x)$ until I've defined it.  So I need to define a co-domain before the image makes sense.  
The other good reason is that we're often interested in sets of functions.  So I might talk about $C^1(\mathbb R)$ which is the set of functions $\mathbb R\to\mathbb R$ with a continuous first derivative.
To construct the set I'm interested in we need to talk about the co-domain. My function $f\in C^1(\mathbb R)$ but it wouldn't be if I had to define $C^1(\mathbb R)$ as functions whose image was $\mathbb R$.    
So although the image can be important the co-domain is necessary too.
A: I think the point of a co-domain is so we can say things like


*

*$f : X \to Y$ is a surjection, as in Samuel's answer

*$j : V \to M$ is an elementary embedding (of structures)

*$f : X \to Y$ is an embedding (of topological spaces).
Considering a function as a statement about ordered pairs, these statements are not just statements about $f$, but also about the given codomains. If we wanted to say the same things without using the notion of co-domain, we could say things like


*

*$f$ is a function on $X$ whose range is $Y$

*$j$ is an embedding from $V$ to an elementary substructure of $M$

*$f$ is a homeomorphism from $X$ to a subspace of $Y$.
However, such statements are often more cumbersome than the natural formulations using co-domains.
A: The codomain tells us what possible values a function may take and the image tells us what actual values it takes. We can have many functions to the same codomain and we may distinguish them by their images.
For example, the codomain may represent the total set of names for all objects in some limited context while an individual map may be an assignment of names to a set of particular objects.
A: As I see it, the codomain of a function $f$ sets up a context in which the function operates, and tells you what sort of operations you can use to build and inspect the value of $f(x)$. So if I have that $f : \mathbb{R} \to \mathbb{R}$, then I know I can use addition, multiplication, division, order structure, topological structure, etc. when producing the outputs of $f$.
It might help if you view $f : A \to B$ not as giving $A$ and $B$ as properties of the function $f$, but merely as a statement "$f$ is a function from $A$ to $B$", that is to say, if I know $x \in A$ I can deduce that $f(x) \in B$. Then I have that if $B \subset C$ then $f : A \to C$ is also true. In this sense there is no single codomain; what I will call the codomain depends on what statements of the form $x \in A \implies f(x) \in B$ I can prove or need to prove.
Think also of "$f$ is a group homomorphism from $A$ to $B$", where it's really necessary to specify $B$ to say what it even means for $f$ to be a group homomorphism, likewise "continuous map of topological spaces" and various other examples. Functions frequently have nice properties that are defined with reference to some set or structure, and then when we need to discuss that particular structure, that's the codomain we'll use.
A: The reason for this discrepancy is that for many functions it's not particularly important what its image is, while at the same time it would be tricky to figure this out.  The codomain however is easy, it describes the type of value one can get. Is it a real number, a complex number, a fraction?
For example, $$f(x)=e^{x\bar{x}}+\frac{7}{3^{x\bar{x}+4}}$$
It's true that $f:\mathbb{C}\rightarrow\mathbb{C}$, but in fact $f:\mathbb{C}\rightarrow \mathbb{R}$.  Hence the codomain gives useful information, but the image is difficult to determine.
A: Codomain and Image of a function are two completely different concepts. The codomain of a function often has a structure, like being a topological space or something like this. It will be very inconvenient to put all that structure in the image, which is only a set, and often not such a nice one. Taking your example, one could say that for $x \to \infty$ the function converges to $0$; when you just look at the image of the function it can't converge to $0$ at all because $0$ is not in the image. And definitions like the pointwise addition of two functions will be much broader as it is sometimes hard to say what is the image of the sum of two functions.
