TITLE: How is the codomain for a function defined?
QUESTION [15 upvotes]: Or, in other words, why aren't all functions surjective? Isn't any subset of the codomain which isn't part of the image rather arbitrary?

REPLY [6 votes]: Here is an answer which directly addresses the question in the title:  the codomain has to be given as part of the information telling you what the functions is; it can't be deduced (or in the language of question, "defined") if all you know is the domain and values of
the function.  It is an extra piece of data.  (This is why it seems arbitrary to you; you are thinking about how to determine the codomain from the other data, which can't be done!
You have to be told what it is as part of the initial description of the function.)
First note that this is incompatible with one traditional definition of a function as being a set of ordered pairs.  The set of ordered pairs definition determines the domain and values of the function, but not the codomain.   (I guess that some people do use this definition of function; for them, a function doesn't have a codomain separate from its image.)
To define a function which has a domain and a codomain, one should instead use the scheme
described by Ryan: a function is a triple (domain $A$, codomain $B$, set of elements of
$A\times B$ determining its values).
As to why we introduce the concept of codomain, Qiaochu's answer describes this.