Is the smoothness required in the following question ? I think continuity is sufficient to prove the function being continuous.
My attempt:
As $g$ is a smooth function , $g$ will also be a continuous function.
Now if $g$ was not a constant function , it would have taken two values $1 , -1$. So $g^{-1}(-1 )$ and $g^{-1}(1 )$ are two disjoint open sets of $S$. Besides , $g^{-1}(1) \cup g^{-1}(-1) = S$ as the co-domain set of $g$ contains only $1 , -1$.
So $S$ is not connected.
But it is given that $S$ is a connected surface of $\mathbb R^n$.
Therefore , $g$ is a constant function.