Help in showing that $f:M\rightarrow \mathbb{R}$ has atleast two critical points Here is the question:

Let M be a compact smooth manifold, $f:M\rightarrow\mathbb{R}$ be a smooth non-constant function. Show that $f$ has at least two critical points. 

I am trying to show by contradiction. Suppose $f$ has less than two critical points. First assume it has none. By Inverse Function Theorem, for $x\in M$, there is an open neighbourhood $U $ containing $x$ such that $f|_U$ is a diffeomorphism. This means $f$ is an open map. Thus, $f(M)$ is open. But this is contradiction to the fact that $f(M)$ is compact (since $f$ is cont's and $M$ is compact). 
Is this fine? Now I don't know how to show that there cannot be only one critical point. Only after proving this, I will be done. Some-where I should be using that $f$ is non-constant. I don't know how. I understand that if $f$ is a constant function, then every point in $M$ is trivially critical. 
Intuitively I don't understand why compactness is used. I am trying to find an example of non-compact smooth manifold $M$ and a smooth map $f:M\rightarrow \mathbb{R}$ that has no critical points. I will appreciate any hint for this too. 
 A: Since $M$ is compact and $f:M \to \Bbb R$ is continuous, there exists points $p_\sigma,  p_\gamma \in M$ such that $f(p_\sigma) = \sigma$ and $f(p_\gamma) = \gamma$, where $\sigma$ and $\gamma$ are the global minimum and maximum values of $f$ on $M$.  This is a basic result of elementary topology.  Since $p_\sigma$ is a global minimum for $f$, we have $df_{p_\sigma} = 0$ and likewise $df_{p_\gamma} = 0$, since $p_\gamma$ is a global maximum.  Since $f$ is not constant, $\sigma < \gamma$ whence $p_\sigma \ne p_\gamma$.  Thus $p_\sigma$ and $p_\gamma$ are distinct critical points of $f$; we see there are at least two critical points of $f:M \to \Bbb R$.  QED.
Hope this helps.  Cheers,
and as always,
Fiat Lux!!!
A: Let's suppose that $M$ is connected. The general case then follows by considering the restriction of $f$ to any connected component of $M$. Then $f(M)$ is a connected compact subset of $\mathbb R$, and since we're assuming $f$ is nonconstant, $f(M)$ is not a single point. What must $f(M)$ look like?
For a counterexample in the case that $M$ is not compact, you could take $M = \mathbb R$ and $f(x)=x$.
