find $f\circ f(x)$ for this funtion 
Find the $( f\circ f)(x)$, if 
  $  f(x) =
\begin{cases}
1+x,\,\,\,\,\,\,\,\,\,\,\ 0\leq x\leq2 \\
3-x,\,\,\,\,\,\,\,\,\,\,\, 2<x\leq3
\end{cases}$

My attempt:
$$
f(f(x))=\begin{cases}1+(1+x),\,\,\,\,\,\,0\le x \le2\\
3-(3-x),\,\,\,\,\,\,2<x\le3\end{cases}
$$
but it is wrong and I don't know how to proceed. Any help will be appreciated!
 A: If $x \in [0,1]$, then $f(x)\in [0,2]$, so $f(f(x))=f(1+x)=1+(1+x)=2+x$.
If $x \in ]1,2]$, then $f(x)\in ]2,3]$, so $f(f(x))=f(1+x)=3-(1+x)=2-x$.
If $x \in ]2,3]$, then $f(x)\in [0,2]$, so $f(f(x))=f(3-x)=1+(3-x)=4-x$.  
Finally : 
$$f(f(x))=\begin{cases}2+x&0\le x \le1\\
2-x&1< x \le2\\
4-x&2< x\le3\end{cases}$$
A: Notice that $f$ is defined only on $[0,3]$. In order to be able to compose it with itself, it is necessary that its range be included in $[0,3]$, therefore let us investigate its range.


*

*If $x \in [0,2]$, then according to the first branch of the definition you have that $f(x) \in [1,3]$.

*If $x \in (2,3]$, then the second branch tells us that $f(x) \in [0,1)$.
Therefore, indeed, $f([0,3]) = [0,3]$ in the following way:
$$f([0,1]) = [1,2], \quad f([1,2]) = [2,3], \quad f((2,3]) = [0,1) .$$
We deduce, then, that $0 \le f(x) \le 2$ means $0 \le f(x) < 1$ or $1 \le f(x) \le 2$, so $x \in (2,3]$ or $x \in [0,1]$, i.e. $x \in [0,1] \cup (2,3]$.
Similarly, $2 < f(x) \le 3$ means $x \in (1,2]$.
It is immediate, from the definition of $f$, that
$$(f \circ f) (x) = f(f(x)) = \begin{cases} 1 + f(x), & 0 \le f(x) \le 2 \\
3 - f(x), & 2 < f(x) \le 3 \end{cases} .$$
Using the considerations about the range of $f$, this can be rewritten as
$$(f \circ f) (x) = \begin{cases} 1 + f(x), & x \in [0,1] \cup (2,3] \\
3 - f(x), & x \in (1,2] \end{cases} .$$
It is clear, then, that it is natural to split out definition of $f \circ f$ in three branches:
$$(f \circ f) (x) =\begin{cases} 1 + (1 + x), & x \in [0,1] \\
1 + (3 - x), & x \in (2,3] \\
3 - (1 + x), & x \in (1,2] \end{cases} .$$
More nicely written, this means
$$(f \circ f) (x) =\begin{cases} 2 + x, & x \in [0,1] \\
2 - x, & x \in (1,2] \\
4 - x, & x \in (2,3] \end{cases} .$$
