Can continuous real-valued function on a countably compact space attain its maximum and minimum?

It is said in my textbook that a continuous real-valued function on a countably compact space can attain its maximum and minimum. However, the proof is not given. I cannot make a proof, and have done some search but failed to find any related consequence.

So I wonder is the proposition true? Anyone can provide a proof?

• Unless you give further information, as stated the answer is trivially and obviously yes: Just take any constant function. That will be an example of a continuous function attaining both its minimum and maximum. – kahen Jul 2 '13 at 5:54
• @kahen: Presumably the OP needs a proof for any continuous function? – copper.hat Jul 2 '13 at 5:56
• Then he shouldn't have written "can [my emphasis] attain its maximum and minimum". – kahen Jul 2 '13 at 6:03
• @kahen: Though his English is quite good, I’m pretty sure that English is not the OP’s first language. This use of can doesn’t work in standard English, but it’s well within the range of near misses by translation. – Brian M. Scott Jul 2 '13 at 6:18

Let $X$ be countably compact, and let $f:X\to\Bbb R$ be continuous. Suppose that $f$ is bounded, and let $u=\sup\{f(x):x\in X\}$. Suppose that $u$ is not in the range of $f$, so that $f$ does not achieve its maximum. For each $n\in\Bbb Z^+$ let
$$U_n=\left\{x\in X:f(x)<u-\frac1n\right\}\;;$$
then $\mathscr{U}=\{U_n:n\in\Bbb Z^+\}$ is a countable open cover of $X$, so it has a finite subcover $\mathscr{U}_0$. Let $m=\max\{n\in\Bbb Z^+:U_n\in\mathscr{U}_0\}$; $U_n\subseteq U_{n+1}$ for each $n\in\Bbb Z^+$, so $X=\bigcup\mathscr{U}_0=U_m$, and therefore $f(x)<u-\frac1m$ for every $x\in X$, contradicting the choice of $u$. Thus, if $f$ is bounded, it must attain a maximum value. Applying this result to $-f$, we see that a bounded function must also attain its minimum value. Thus, we’re done if we can show that $f$ must be bounded.
This can be done with the same kind of argument. For each $n\in\Bbb Z^+$ let $U_n=\{x\in X:f(x)<n\}$, and let $\mathscr{U}=\{U_n:n\in\Bbb Z^+\}$; then $\mathscr{U}$ is a countable open cover of $X$, and $U_n\subseteq U_{n+1}$ for each $n\in\Bbb Z^+$, so there is an $m\in\Bbb Z^+$ such that $X=U_m$. But then $f(x)<m$ for each $x\in X$, and $f$ is bounded above. Since $-f$ is also bounded above, $f$ is bounded below and therefore bounded.