For the security of the dead
In Britain, prior to 1832, the only legal source of human corpses for dissection and the teaching of human anatomy were those of executed murderers. Despite the fact that the 1752 Murder Act made penal dissection mandatory for convicted killers, there was a great shortage of bodies for dissection. This shortfall was made good by body-snatchers who dug up newly buried bodies for the anatomy schools.
In an attempt to prevent grave robbing many kirkyards were guarded overnight by local volunteers for several weeks after a burial, until the body was too corrupt to be of interest to the anatomists. The Kirk Session often built a watch-house in or adjacent to the graveyard to provide shelter for the watchers.
Church Sessions, burial societies and other organisations also took measures to try to prevent the resurrectionists from entering the graveyard in the first place. The obvious way to do this was to fortify the perimeter and his approach was widely adopted, in Scotland and elsewhere. This is the massive 12 foot high wall protecting the graveyard at Foveran Parish Church.
The graveyard is also furnished with a watch-house which I have blipped on a previous occasion.