There was a time when states were very different from what they are now. Borders did not exist as they do now. The territory of an empire was basically the range where it could impose its influence. If someone invaded, it could easily happen that a foreign army suddenly stood in front of your capital city.
Take the Eastern Roman Empire for instance. It encompassed all shores of the eastern Mediterranean, Syria, Palestine and Egypt. When Mohammed died in 632, there was still only a fragile islamic state on the Arabian peninsula. In 654, the Arabs had conquered Egypt, and in 717 an Arab fleet and army besieged Constantinople. They stayed for a year, but couldn’t conquer the City. The whole of Anatolya, all of Greece, much of the Balkans and parts of the middle east coasts stayed within the empire or were re-conquered.
It was more than 700 more years, that the Byzantine Empire endured, and much of the time still as an enormous force to be reckoned with.
War was elastic, the population was thin, soldiers were few. No army could have secured the extended borders of the Byzantine Empire. It had been possible at the height of Roman power, but once that had waned, no empire could have financed or even just manned such huge armies. Even Rome crumbled under the pressure of barbarian migration.
That’s where castles came in. A classic castle is a fortress, a military installation, designed to endure the storm of freely wandering armies. An army has to be fed and kept healthy. Besieging a castle or a fortified town was an exceedingly expensive endeavor. Therefore it was a defenders best bet to just close up behind the doors of a castle, and wait for the storm to go away.
This is Budapest’s “castle”. You see, it’s from another time, it’s a castle only in name. It’s a palace.