What is the right way to define a function?
One way to proceed is the following, starting from the concept of ordered couple, defined set-theoretically in the usual way.
We define when a set is a relation :
$R \text { is a relation } \ ↔ ∀z(z \in R → ∃x \ ∃y \ (z = \langle x,y \rangle \ ))$.
Thus, a relation is a set of ordered couples.
As usual, we write $xRy$ for $\langle x,y \rangle \in R$.
We define :
$\mathcal {Dom}_R = \{ x : \exists y \ (x R y) \}$
and :
$\mathcal {Img}_R = \{ y : \exists x \ (x R y) \}$.
Then we define when a relation is a function :
$f \text { is a function } ↔ f \text { is a relation } \land ∀x \ ∀y \ ∀z \ (xfy ∧ xfz → y=z)$.
Up to now, no "$A$s" nor "$B$s".
Then we say :
$f \text { is a function from } A \text { into } B \leftrightarrow f \text { is a function } \land \mathcal {Dom}_f=A \land \mathcal {Img}_f \subseteq B$;
$f \text { is a function from } A \text { onto } B \leftrightarrow f \text { is a function } \land \mathcal {Dom}_f=A \land \mathcal {Img}_f = B$.
See : Patrick Suppes, Axiomatic set theory (1960), page 88.
See also : Kenneth Kunen, The Foundations of Mathematics (2009), page 24-25.
Regarding your example, with this approach we have that $f=g$, where $f$ is a function from $\{ 0,1 \}$ into $\mathbb R$, while $g$ is a function from $\{ 0,1 \}$ onto $\{ 2,3 \}$.
In this case, surjectivity is not an "intrinsic" property of $g$; the applicability of the word "onto" depends both on $g$ and on the set $B$, not just on $g$.
A different approach is possible, of course; see :
- Robert Goldblatt, Topoi : The Categorial Analysis of Logic (2ed - 1984) , page 19 :
modify the definition of function in the following way. Firstly for sets $A$ and $B$ we define the product set or Cartesian product of $A$ and $B$ to be the set of all ordered pairs whose first elements are in $A$ and second elements in $B$. This is denoted $A \times B$, and so
$$A \times B = \{ \langle x, y \rangle : x \in A \land y \in B \}.$$
A function is now defined as a triple $f = \langle A, B, R \rangle$, where $R \subseteq A \times B$ is a relation from $A$ to $B$ (the graph of $f$), such that for each $x \in A$ there is one and only one $y \in B$ for which $\langle x, y \rangle \in R$. Thus the domain ($A$) and codomain ($B$) are incorporated in the definition of a function from the outset.
With this approach, the two "objects $\langle \{ 0,1 \}, \mathbb R, f \rangle$ and $\langle \{ 0,1 \}, \{ 2,3 \}, f \rangle$ are not the "same" function.
In this second case, surjectivity is an "intrinsic" property of $\langle \{ 0,1 \}, \{ 2,3 \}, f \rangle$.