If $(f\circ g)(x)=x$ does $(g\circ f)(x)=x$? Given  $$(f◦g)(x)=x$$ (from R to R for any x in R)  
does it mean that also $$(g◦f)(x)=x$$
I feel like its not true but I can't find counter example :(
I tried numerous ways for several hours but I cant counter it though I almost know for sure that this will only be true if g is onto but I don't know why :P

Let $f(x)=\begin{cases}\tan(x),&\text{if }x \in (-\pi/2,\pi/2)\\0,&\text{otherwise}\end{cases}$
and $g(x)=\tan^{-1}(x)$ with image $(-\pi/2,\pi/2)$.
Then $f(g(x))=x$ but $g(f(2\pi))=0\ne 2\pi$.
The hypothesis is indeed true if both $f$ and $g$ are continuous. $g(\mathbb{R})$ is an interval, and $f|_{g(\mathbb{R})}$ has inverse* $g$ and hence is injective, so must be strictly monotone. If $g(\mathbb{R})$ has a finite endpoint, e.g. $(-\infty,t)$, then $\lim_{x\to t^-} f(x)=\pm\infty$, otherwise $f$ cannot have image $\mathbb{R}$. This contradicts the assumption that $f$ is continuous. Hence $g$ is surjective, and so $f$ and $g$ must be inverses of each other.
*To show that $\forall x\in g(\mathbb{R}), g(f(x))=x$:

Let $y\in \mathbb{R}$ s.t. $g(y)=x$.
$g(f(x))=g(f(g(y)))=g(y)=x$.

I think you can come up with a counterexample if we have only $f$ continuous.
