Joels answer made me think a bit and I believe I found an interesting solution for $f(x)$ :

$ f(x) = \begin{cases} ix & \text{if } Im(x) = 0, x\neq 0 \\\ \cos(ix) & \text{if } Re(x) = 0,x \neq 0 \\\ 2\pi i & \text{if } x = 0 \end{cases}$

It is of course a bit of a trick (reminds me of [Wick Rotation][1]), but I it works for all $x\ \epsilon\ R$, because

$f(f(x)) = \cos(i(ix))=\cos(-x) = \cos(x)$

Update: Added the case $x=0$. For this we have

$f(f(0)) = \cos(i(2\pi i))=\cos(-2\pi) = \cos(0)$


  [1]: http://en.wikipedia.org/wiki/Wick_rotation