Classical density functional theory for finite temperatures is usually formulated in the grand-canonical ensemble where arbitrary variations of the local density are possible. However, in many cases the systems of interest are closed with respect to mass, e.g. canonical systems with fixed temperature and particle number. Although the tools of standard, grand-canonical density functional theory are often used in an ad hoc manner to study closed systems, their formulation directly in the canonical ensemble has so far not been known. In this work, the fundamental theorems underlying classical DFT are revisited and carefully compared in the two ensembles showing that there are only trivial formal differences. The practicality of DFT in the canonical ensemble is then illustrated by deriving the exact Helmholtz functional for several systems: the ideal gas, certain restricted geometries in arbitrary numbers of dimensions and finally a system of two hard-spheres in one dimension (hard rods) in a small cavity. Some remarkable similarities between the ensembles are apparent even for small systems with the latter showing strong echoes of the famous exact of result of Percus in the grand-canonical ensemble.