$\nabla U=0 \implies U=\mathrm{constant}$ only if $U$ is defined on a connected set? How can I prove the following statement?

Given a function $U: A \subset \mathbb{R}^n \to \mathbb{R}$ with $A$
  connected set, $$\nabla U=\bar{0} \,\,\,\,\,\,\, \forall \bar{x} \in A\implies U=\mathrm{constant} \,\,\,\,\,\,\, \forall \bar{x} \in
A$$

In particular I do not understand how the condition of $A$ as a connected set is necessary to prove the statement.
 A: The zero gradient condition implies that the function is locally constant:


*

*Why does zero derivative imply a function is locally constant?

*A function with zero gradient is locally constant
Together with the connectedness assumption, you have that the function is constant:
Locally Constant Functions on Connected Spaces are Constant

To see why connectedness is necessary, consider $A$ being a disjoint union of two open balls and the function $U$ being different constants in each ball. 

One might be also interested in the following related question:  
Can locally "a.e. constant" function on a connected subset $U$ of $\mathbb{R}^n$ be constant a.e. in $U$?
A: If $A$ is connected and open and $x,y\in A$ are arbitrary with $[x,y]:=\{ ta+(1-t)b \mid t\in [0,1]\}\subset A$, there exists a number $t_0 \in (0,1)$ with
$$U(y)-U(x)= \nabla U (x+t_0(y-x))\cdot(y-x) .$$
Pick $a,b \in A$ and link them with a polygon with vertex points $x_1, \dotsc, x_k$ ($k\in \mathbf N$) where $x_1 =a$ and $x_k=b$. Applying the intermediate value theorem on $[x_{j-1},x_{j}]$ ($j=2, \dotsc, k$) gives
$$U(a)-U(x_2)=U(x_1)-U(x_2)=0 \qquad \dotsc \qquad U(x_{k-1})-U(x_k)=U(x_{k-1})-U(b)=0$$
since $\nabla U=0$ and hence
$$U(a)=U(x_1)= \dotsc = U(x_{k-1})=U(x_k)=U(b).$$
Since $a$ and $b$ were arbitrary we showed that $U$ is constant.
A: As you have two different components, your function can be constant at each component but as a function it is not constant and its gradient is $0$.
