Generally they had two primary functions:
1. They were used as defensive fortified structures in times of war. The medieval ages were a time of weapons far more crude than the ones used in modern warfare, so a high stone wall and thick gate would sometimes make the difference between life or death for a king or his vassals. They were by no means impenetrable, and castles were won and lost all throughout medieval history, sometimes even surrendered without an actual battle taking place. But they helped give a possibly outnumbered army a fighting chance against a larger army, and a strong castle was sometimes enough to deter a weaker force. In times of war they also sometimes provided sanctuary to peasants from the nearby countryside that would otherwise have been slaughtered by invading armies. They often included several defensive tactics and technologies depending on the size and design of the castle, and the competition between building a better castle and building better castle-destroying siege weapons was one of the main arms races of the age (along with better armor vs better swords or arrows, etc). For these reasons many castles were built in highly strategic locations, both geographically in areas they could provide a measure of control over important territories or logistics routes and physically in places where nature added some sort of extra measure of defense (a large river or cliff edge, for example).
2. Other than that, some castles served as lavish homes for royalty or their vassals (important noblemen who served the king). Some castles contained Keeps that could serve as palaces for royalty or homes for other nobility. In this sense a castle could be given as a great reward to important vassals for their loyalty or some great service. Some kings built bigger and more luxurious castles as testaments to their wealth and power.
Some castles were designed to be both important military fortresses and lavish homes, while others may have been built with a greater focus on one function or the other. Either way most castles probably ended up serving as both at one point or another.
Who is most important in medieval history - kings and emperors. Where do they live - in castles.
for defense
Medieval castles.
To protect the king
There is nothing different between todays castles and medieval time castles. Today's castles were built in medieval times.
Medieval England
The welsh medieval castles were big and gray.
They were a great retreat against invasions and or intrusions! : P
Castles have stables to keep animals and horses were so important because they needed them fr when there was an attack.
To defend and protect people and important places of course.
Medieval times
in the late medieval period many medieval castles did have a primitive type of cannon.