Great wall of China. Hadrian’s wall. Vatican’s wall. Obama’s wall. Pelosi’s wall. Paul Ryan’s wall. etc.
One of the main purposes of border Walls, which are older than the Middle Ages by far, is to provide not only a barrier to enemies crossing the border, but a transportation avenue for your border guards responding to an unauthorized crossing (i.e. an invasion). Which implies another factor: Border Walls require garrisons of Border Guards.