This question is based on question $3.11$ from chapter $2$ of Hartshorne, found on page $92$.
Part $a)$ of said question asks to show that closed immersions are stable under base extension. In other words, if $f:Y \rightarrow X$ is a closed immersions and $g:X'\rightarrow X$ a morphism of schemes, then the induced map $f':Z=Y\times_X X' \rightarrow X'$ is also a closed immersion.
My question is:
How can we prove this without using that a closed immersion is affine?
I ask because although I can reduce to the case where $X'$ and $X$ are affine fairly easily, I have not been able to find a proof anywhere (including on related questions such as this one on the site) that doesn't use part $b)$ of the question in Hartshorne, namely that if $X = \operatorname{Spec}A$ then $Y$ is affine, and in fact is the closed subscheme determined by some ideal of $A$. Without this, I can't see how to reduce to the affine case since even if $X$ is affine, the restriction of $f$ to an arbitrary affine piece of $Y$ need not be a closed immersion.
Although part $b)$ doesn't rely on part $a)$ and we therefore could use it to prove part $a)$, I can't help but think that if this is what Hartshorne had in mind then he would have switched the order around. This is the first time that I've had to use a later exercise to prove an earlier one and would like to avoid it if at all possible.
Of course it's pretty much impossible to prove that such a proof doesn't exist. I would however accept an answer from someone who feels that they have enough experience to say that if such a proof exists they would most likely have seen it, or who can provide some reasonably convincing heuristic argument that it can't be done.
As is usual with questions involving base change, one approach is to try to phrase everything in terms of universal properties, i.e., to ask "what functor does $f:Y\to X$ represent?" In this case, the answer is $$\text{Schemes}_{/X}\to \text{Sets}$$ defined by sending $h:A\to X$ to the set of $\mathcal{O}_X$-module homomorphisms $\mathcal{O}_X/\mathcal{I}_Y\to h_*\mathcal{O}_A$, where $\mathcal{I}_Y$ is the ideal sheaf of $Y$. In terms of universal properties, this says $f:Y\to X$ is characterized up to unique isomorphism by the fact that given any $h:A\to X$ such that $h^\sharp:\mathcal{O}_X\to h_*\mathcal{O}_A$ factors through $\mathcal{O}_X/\mathcal{I}_Y$, there exists a unique homomorphism $\phi:A\to Y$ such that $h = f\circ \phi$.
Once this has been established, we can check that $f':Z\to X'$ satisfies this property. Given $h:A\to X'$ such that $h^\sharp$ factors through $\mathcal{O}_{X'}/\mathcal{I}_Z$, we have that $(g\circ h)^\sharp$ factors through $\mathcal{O}_X/\mathcal{I}_Y$ by commutativity of the pullback square. As $f:Y\to X$ is a closed immersion, there is a unique $\phi:A\to Z$ such that $g\circ h = f\circ \phi$. By the universal property of base extension, there exists a unique $\psi:A\to Z$ such that $f'\circ \psi = h$ and $\mathrm{pr}_1\circ \psi = \phi$. This $\psi$ shows that $f'$ satisfies the above "universal property of closed immersions".
Of course, it remains to show that the claims in the first paragraph are correct. I won't give too many details, but the idea is that given $h:A\to X$ with $h^\sharp$ factoring through $\mathcal{O}_X/\mathcal{I}_Y$, one first shows that $h(A)\subseteq f(Y)$, giving a continuous map $\phi:A\to Y$, and then obtains $\phi^\sharp:\mathcal{O}_Y\to \phi_*\mathcal{O}_A$ by adjunction from $\mathcal{O}_X/\mathcal{I}_Y = f_*\mathcal{O}_Y \to h_*\mathcal{O}_A$, using the facts that (1) $f^*f_*\mathcal{O}_Y\to \mathcal{O}_Y$ is an isomorphism in the case of closed immersions and (2) $h_*\mathcal{O}_A \cong f_*\phi_*\mathcal{O}_A$ canonically.