The answer to that question is contested. However, the main purpose of castling is to bring your king to safety while simultaneously activating your rook.