TITLE: If $(f\circ g)(x)=x$ does $(g\circ f)(x)=x$?
QUESTION [7 upvotes]: 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

REPLY [3 votes]: Define:
$$f(x)=\begin{cases}
x&\text{ if }x\le0,\\
0&\text{ if }0\le x\le1,\\
x-1&\text{ if }x\ge1;
\end{cases}$$
$$g(x)=\begin{cases}
x&\text{ if }\,x\lt0,\\
x+1&\text{ if }\,x\ge0.\\
\end{cases}$$
Then $f$ is continuous, $f(g(x))=x$ for all $x$, and $g(f(0))=1$.