The primary function of the respiratory system is to facilitate the exchange of gases, mainly oxygen and carbon dioxide, between the body and the environment.