When do two functions become equal?

The problem is that there are two slightly different conventions about what “function” means. The more common one is that, for any function, the codomain has to be specified. So, if two functions have different codomains (e.g. $\Bbb R$ and $\Bbb C$ in your example), then they are different functions, even if they have the same graph. The other convention is to identify the function with its graph, so that any set $f$ of ordered pairs $(x,y)$ such that $y_1=y_2$ whenever $(x,y_1)\in f$ and $(x,y_2)\in f$ is a function. This latter convention is elegant, and does away with the seemingly unnecessary codomain. However, the property of being “onto” becomes relative—onto what?—and the idea of a function being a surjection cannot be used. This matters in some branches of mathematics but not in others.


You're mixing up range with codomain.

If $f:X \to Y$, then $Y$ is the codomain; the intuitive definition is that $Y$ tells you what type of values $f(x)$ has ($f(x)\in Y$). The range is the actual set of values of $\{f(x) \mid x \in X\}$.

If you have $f$ represented as a set of ordered pairs, you can find the domain and range, but you can't find the codomain $Y$. (What's the codomain for the function $\{ (1,1) \}$? You might say $\{1\}$, or $\mathbb Z$, or $\mathbb Q$, etc.) Hence, $Y$ must be specified.

Now when we get back to the question "when are functions $f$ and $g$ equal?", the answer is: a function does not depend on its relation $R_f$; a codomain $Y_f$ must also be supplied. Then $$f=g \iff (R_f = R_g \wedge Y_f = Y_g).$$ The equivalence relation $$f\sim g \iff R_f = R_g$$ is hence useful most of the time, but not always (as you pointed out).


Most of the time, we don't really care about the precise set theoretical definition of a function $f \colon X \to Y$ and this is the cause of your confusion.

In order to answer your question, let me talk about one possible definition in ZF.

For sets $a,b$ we define their ordered pair as $(a,b) := \{ \{a\}, \{a,b\} \}$. Feel free to prove that $(a,b) = (c,d)$ if and only if $a=c$ and $b=d$.

By induction on the number of entries, we define a $(n+1)$ tupel by $(a_1, a_2, \ldots, a_n, a_{n+1}) := ((a_1, \ldots, a_n), a_{n+1})$. Again we can prove $(a_1, a_2, \ldots, a_n) = (b_1,b_2, \ldots, b_n)$ if and only if $a_1 = b_1, \ldots, a_n=b_n$.

We define $X_1 \times \ldots \times X_n := \{ (x_1, \ldots, x_n) \mid x_1 \in X_1, \ldots, x_n \in X_n \}$. Finally, write $f \colon X \to Y$ and call $f$ a function if $f \subseteq X \times Y$ and for all $x \in X$ there is a unique $y \in Y$ s.t. $(x,y) \in f$. We write $f(x)$ for this unique $y$.

Using this definition (which is probably the "standard" one), one can easily read off the domain of $f$, namely $\operatorname{dom} f = \{ x \mid \exists y \in Y \colon (x,y) \in f \}$ and similarly one can read off the image of $f$.

However, one can not read off the co-domain of $f$. In fact, given two functions $f,g$ with domain $X$ s.t. $f(x) = g(x)$ for all $x \in X$, we indeed have $f = g$ as sets, no matter what their co-domains are (as long as they include the image of $f$). As a result of this, the statement "$f$ is surjective" doesn't really make sense, while "$f$ is surjective on $Y$" does.

If one considers this to be undesirable, there is an easy fix: Define a function $f \colon X \to Y$ to be a subset $f \subseteq X \times Y \times \{Y \}$ s.t. for each $x \in X$ there is a unique $y \in Y$ s.t. $(x,y,Y) \in f$. This works fine as long as one doesn't want to differentiate between functions with empty domain.