During the Middle Ages, the castle was the fortified residence of a Feudal Lord or the Monarch and members of his Court.
At that time, Europe was in an almost constant state of warfare and the main purpose of castles was to withstand attack and siege. So once the castle had been built, workers dug a deep ditch along the outside of the walls and filled it with water. This became the Castle Moat, which was another defence against attack but also a source of fish for the Nobles' table.
As well as protecting the King or Lord and their people, castles also served as a prison, as safe place for storing treasures, as arsenals for weapons and the machinery of warfare and as administrative centres for the surrounding area.