An idempotent function $\mathbb R \to \mathbb R$ I did an exam yesterday and this was one of the questions. I didn't have enough time to fully answer it so I wonder what the solution is:

Let $f: \mathbb R \to \mathbb R$ be a continuous function with $f \circ f = f$. Show:

*

*The image of $f$ is a closed interval (of course it may be unbounded).

*If $f$ is differentiable then it is either the identity or a constant function.


I know if $f$ is differentiable, then by the chain rule:
$$f'(x) = (f \circ f)'(x) = f'(x)\cdot f'(f(x)). $$
That implies $f'(x) = 0$ or $f'(f(x)) = 1$ but it may be different depending on $x$.
 A: A differentiable function whose derivative is either 0 or 1 has constant derivative, in view of Darboux property: if the derivative has value 0 and 1 then it has also all the intermediate values.
A: Answer to 1.
A continuous function is such that given any sequence $(x_n)$ such that $x_n\to x$, then  $f(x_n)\to f(x)$.
First, note that if $f$ takes two values $f(x_1), f(x_2)$, because of the intermediate value theorem, it must take all the values in between. This means that the image is an interval with possibly infinite endpoints.
To see why the interval is closed, suppose that $y$ is a limit point of the image, i.e. there exists a sequence of values $f(x_n)$ wich converges to $y$. The continuity of $f$ implies $f(f(x_n))\to f(y)$, so that $f(x_n)\to f(y)$ by idempotency. Since the limit is unique, this means that $f(y)=y$, so that $y$ is part of the image.
This proves that the image is a closed interval. I think the infinite endpoints must be allowed, take for instance $f$ to be the identity function: then the image is $\mathbb (- \infty,\infty)$.
Answer to 2.
Your result says that at each point $x$, either $$f'(x) = 0 \text{ or } f'(f(x)) = 1. \label{a}\tag{1}$$
Evaluating this last statement at $y = f(x)$ and applying idempotency, we get that either $f'(y) = 0$ or $f'(y) = 1$ at each point $y$ in the image of $f$.
By part 1, we know that the image $I$ must be a closed interval.
If $I$ contains just one point, then $f$ is a constant. Otherwise, we know that $I$ is a proper interval, and $f | _I$ is either a constant or the identity in virtue of Darboux's theorem following Emanuele Paolini's answer (since no other linear functions are idempotent).
We can readily rule out the case where $f|_I$ is a constant.
This would imply $f'(f(x)) = 0$ for all $x$ and so, by $\ref{a}$, $f'(x) = 0$ for all $x$ and $I$ reduces to a point.
If $f|_I$ is the identity, we will argue that $I = \mathbb R$.
Suppose the image of $f$ has an end on the right, $b$.
Then $b$ is the maximum value of $f$. Taking the limit from the left as $x \to b$ we find that $f(b) = b$, i.e. it takes its maximum value at $x=b$, which implies $f'(b) = 0$ by the extremum value theorem. However $f'(b) = 1$ taking the limit from the left, a contradiction. The argument is analogous for a lower bound $a$. Thus, $I=\mathbb R$ and $f$ is the identity.
A: I now have the following for the case that $f$ is differentiable and surjective:
Take an $x \in \mathbb R$. It can be written as $f(y)$. Then either $f'(x)=0$ or $f'(f(x)) = f'(x) = 1$. Because of the Darboux property, only one of those can hold for all $x$ at once. $f'(x)=0$ is impossible because then $f$ would be constant and not surjective. So it must be $f'(x)=1$, i.e. $f(x)=x+c$. By idempotence $c=0$.
