The major function of the lungs is gas exchange between the lungs and the blood. The alveolar and pulmonary capillary gases equilibrate across the thin blood–air barrier. This thin membrane (about 0.5 –2 μm thick) is folded into about 300 million alveoli, providing an extremely large surface area (estimates varying between 70 and 145 m2) for gas exchange to occur.
The lungs are not capable of expanding to breathe on their own, and will only do so when there is an increase in the volume of the thoracic cavity. This is achieved by the muscles of respiration, through the contraction of the diaphragm, and the intercostal muscles which pull the rib cage upwards as shown in the diagram. During breathing out the muscles relax, returning the lungs to their resting position. At this point the lungs contain the functional residual capacity (FRC) of air, which, in the adult human, has a volume of about 2.5–3.0 litres.
During heavy breathing as in exertion, a large number of accessory muscles in the neck and abdomen are recruited, that during exhalation pull the ribcage down, decreasing the volume of the thoracic cavity. The FRC is now decreased, but since the lungs cannot be emptied completely there is still about a litre of residual air left. Lung function testing is carried out to evaluate lung volumes and capacities.
The lungs possess several characteristics which protect against infection. The respiratory tract is lined by epithelia with hair-like projections called cilia that beat rhythmically and carry mucus. This mucociliary clearance is an important defence system against air-borne infection. The dust particles and bacteria in the inhaled air are caught in the mucosal surface of the airways, and are moved up towards the pharynx by the rhythmic upward beating action of the cilia. The lining of the lung also secretes immunoglobulin A which protects against respiratory infections; goblet cells secrete mucus which also contains several antimicrobial compounds such as defensins, antiproteases, and antioxidates. Ionocytes regulate mucus viscosity.  In addition, the lining of the lung also contains macrophages, immune cells which engulf and destroy debris and microbes that enter the lung in a process known as phagocytosis; and dendritic cells which present antigens to activate components of the adaptive immune system such as T-cells and B-cells.
The size of the respiratory tract and the flow of air also protect the lungs from larger particles. Smaller particles deposit in the mouth and behind the mouth in the oropharynx, and larger particles are trapped in nasal hair after inhalation.
In addition to their function in respiration, the lungs have a number of other functions. They are involved in maintaining homeostasis, helping in the regulation of blood pressure as part of the renin–angiotensin system. The inner lining of the blood vessels secretes angiotensin-converting enzyme (ACE) an enzyme that catalyses the conversion of angiotensin I to angiotensin II. The lungs are involved in the blood's acid-base homeostasis by expelling carbon dioxide when breathing.
The lungs also serve a protective role. Several blood-borne substances, such as a few types of prostaglandins, leukotrienes, serotonin and bradykinin, are excreted through the lungs. Drugs and other substances can be absorbed, modified or excreted in the lungs. The lungs filter out small blood clots from veins and prevent them from entering arteries and causing strokes.
The lungs also play a pivotal role in speech by providing air and airflow for the creation of vocal sounds, and other paralanguage communications such as sighs and gasps.
New research suggests a role of the lungs in the production of blood platelets.