# Let $f,g$ be functions such that $(g\circ f)(x)=x$ for all $x \in D(f)$ and $(f\circ g)(y)=y$ for all $y \in D(g)$, prove that $g=f^{-1}$

Attempt: I need to basically show that: $$D(f)=R(g)\\ D(g)=R(f)$$

We can clearly infer the following from the information given above: \begin{align} D(f)=R(g\circ f) \subseteq R(g) \tag{1}\\ D(g)=R(f\circ g) \subseteq R(f) \tag{2} \end{align}

And we know trivially from the definition of function composition, \begin{align} R(f) \subseteq D(g) \tag{3}\\ R(g) \subseteq D(f) \tag{4} \end{align}

From $(1),(2),(3)$ and $(4)$, we get out intended result. Is this proof correct? I'd like to know if there is any other way of proving this.

It is correct, but I would do it as follows: $D(g)\subset R(f)$ because, if $x\in D(g)$, then $x=f\bigl(g(x)\bigr)\in R(f)$ and for the same reason, $D(f)\subset R(g)$. For the rest, I would have done it as you did. But it's really just a matter of taste.