Why it is necessary to mention the domain and codomain of function as part of its definition? Relations are defined, without mentioning its domain and codomain, as,

Relation is a set of ordered pairs.

Intuitively a function is a non one-to-many relation. But additionally the definition of function has the domain and codomain in it. One possible definition is,

A function $f$ from $A$ to $B$ is a non one-to-many relation such that $\mathbf{Dom}(f) = A$ and $\mathbf{Ran}(f) \subseteq B$.

Why can't we just say

function is a non one-to-many relation

and then we may introduce the domain and codomain (as we usually do for relations). Afterwards we shall continue to speak about surjection, bijection, inverse, composition, etc.
 A: The domain can be directly extracted from the set of ordered pairs version of the function. It is important enough that we usually prefer to isolate it anyway, but there would be no loss in the domain being a "function" of the function (namely $\operatorname{Dom}(f)=\{ \operatorname{First}(t) : t \in f \}$) rather than a "data field" in the definition of a function (e.g. $\operatorname{First}(f)$).
The codomain cannot be extracted from the ordered-pair version of the function. The range can, of course, but we like to think of the range and the codomain of a function as distinct objects. This simplifies a lot of things. In particular, the notion of surjection makes no sense without it. Moreover it allows us to speak of all functions with a given domain and codomain (e.g. all real-valued functions of a real variable) without regard to their range.
Note that all of the above is still true of relations; they should really be thought of as a triple of domain, codomain, and set of ordered pairs. But the issue is less important in the setting of general relations.
