TITLE: Real-valued function $f : \mathbb{R} \to \overline{\mathbb{R}}$ is continuous iff its graph is closed
QUESTION [5 upvotes]: I'm considering functions  $f : \mathbb{R} \to \mathbb{R}$ with the codomain extended to the extended reals $\mathbb{R} \cup \{\pm\infty\}.$ Now if $f$ is continuous then the function $g : \mathbb{R} \times \overline{\mathbb{R}} \to \mathbb{R} : (x, y) \mapsto y - f(x)$ is also continuous. Then the graph of $f$ is the set $g^{-1}(\{0\})$ and is therefore closed.
It seems plausible that the converse is true - that is, that if the graph of $f : \mathbb{R} \to \overline{\mathbb{R}}$ is closed then $f$ is continuous - but I wouldn't be too surprised if there's an annoying counterexample.
Is the converse true?

REPLY [3 votes]: This is true. Given $[a,b] \subset \mathbb{R}$, $\mathrm{Gr}(f) \cap ([a,b] \times \overline{\mathbb{R}})$ is compact (since it is a closed set inside a compact one). It follows that $\pi_1 |_{\mathrm{Gr}(f) \cap ([a,b] \times \overline{\mathbb{R}})}$ is a homeomorphism with its image, and hence $f|_{[a,b]}=\pi_2 \circ (\pi_1 |_{\mathrm{Gr}(f) \cap ([a,b] \times \overline{\mathbb{R}})})^{-1}$ is continuous. Therefore $f|_{(a,b)}$ is continuous. Since this holds for arbitrary $(a,b) \subset \mathbb{R}$, $f$ is continuous in the whole real line.
Note that this generalizes to the following statement:

Let $X$ be locally compact and Hausdorff, and $Y$ be compact. If $f:X \to Y$ is a function such that $\mathrm{Gr}(f)$ is closed, then $f$ is continuous.