TITLE: I am learning functional equation and encountered $f(f(x)-x)=x$.
QUESTION [8 upvotes]: I was self-learning functional equations because I will be learning functional equation in Math Team soon. And I made up the following problem:

Find all solutions to functional equation $f:\mathbb{R}\rightarrow\mathbb{R}$ $$f(f(x)-x)=x$$

I tried to subsituite $g(x)=f(x)-x$ and got $g\big(g(x)\big)+g(x)=x$. And I can prove that $g$ is injective. The proof is as follows:
If $g(a)=g(b)$, then $$a=g\big(g(a)\big)+g(a)=g\big(g(b)\big)+g(b)=b.$$
But I cannot move further.
As @PaulSinclair pointed out, there is an uncountably infinite collection of extremely ill-behaved solutions (I don't know why so please explain), so assume $f$ is continuous first.
Can someone help me? Any help is appreciated!

REPLY [3 votes]: A few days ago I deleted my answer, which depended on the injectivity of $f$, which is not obvious at all, as Eric Wofsey remarked. An answer has been posted since (by him), but I'll post my variation of the previous answer anyway.
We want to prove that the two functions $f(x) = \lambda x$ where $\lambda$ is a solution to $\lambda^{2}-\lambda-1=0$ are the only two solutions. The idea is as follows:

Consider the graph $\Gamma = \left\{(x,f(x)\,|\,x\in\mathbb R)\right\}$ of $f$ in the plane. The functional equation translates into the existence of a map $T$ that preserves $\Gamma$.
Unless $f(x) = \lambda x$ with $\lambda$ as above, we can use $T$ to construct points $P_1 = (x_1, f(x_1))$ and $P_2 = (x_2, f(x_2))$ with $x_1, x_2$ of the same sign and $P_1, P_2$ on different sides of the line $x = y$.
It follows that there is a point with $x = f(x)$ with $x \ne 0$. However, from the functional equation we see that $0$ is the only possible fixed point.

Now for the details:
Let $(x,y)\in\mathbb R^2$ be a point on the graph $\Gamma$ of $x$ (so $f(x) = y$). Then $(y - x, x)$ also is on the graph of $f$. That means that the linear transformation with matrix
$$T = \begin{pmatrix}-1 & \phantom{-}1\\ \phantom{-}1 & \phantom{-}0\end{pmatrix}$$
maps the graph of $f$ onto itself. It is not hard to see that $T:\Gamma\to\Gamma$ is surjective, so $T^{-1}$ also preserves $\Gamma$.
$T$ is diagonalized by a reflection, and the eigenvalues are $-\phi$ and $\phi^{-1}$.
Let $\Gamma'$ be the graph in the new coordinates, which is a reflection of the original graph. Note that it doesn't have to be the graph of a function anymore. We want to prove that $\Gamma'$ has to coincide with one of the coordinate axes.

Assume that there exists a point $P\in\Gamma'$ outside the (new) axes. Then $T^{2n}P \to (\pm\infty,0)$ and $T^{2n + 1}P \to (\mp\infty,0)$ as $n\to\infty$, since $\phi > 1$ and $\phi^{-1} < 1$. 
This shows that for every $x$ there is a point $(x,y)\in\Gamma'$ for at least one $y\in\mathbb R$. Let $y$ be such that $(0,y)\in\Gamma'$, then also $(0,\phi^{-n}y)\in\Gamma'$, so $(0,0)\in\Gamma'$ by continuity.
Also $T^{-n}P\to (0,\pm\infty)$ where the sign is equal to the sign of the $x$ coordinate of $P$.

We are now in the situation of the following picture, where the red dots lie on the graph, the blue lines are the eigenspaces of $T$. The dots other than the origin may be very far, but they can be arbitrarily close to the blue lines.

Note that $T^{-n}P$ could also be between the negative $y$-axis and the line $y = x$, depending on the location of $P$. In any case it is clear that the graph of $f$ must cross the line $y = x$ at some $x \ne 0$, i.e. $f(\xi) = \xi$ for $\xi\ne 0$. But
$$\xi = f(f(\xi) - \xi) = f(0) = 0$$
showing that such a point $P$ couldn't exist.