The pharynx's main purpose is to allow for air, food, and fluid to travel through until it reaches the epiglottis to be split. Specifically, food is pushed into the esophagus so it's not breathed in. The pharynx is split into three parts, the nasopharynx, the oropharynx, and the hypopharynx/laryngopharynx. Notably, the pharynx also connects to the ears through the eustachian tubes.
The function of swallowing, also known as deglutition is split into two stages for the transport of the bolus - the lump of food that was just ingested.
Oral stage - This stage is voluntary and is when the tongue elevates to move the bolus posteriorly so that liquids are sealed between the tongue and the hard palate and solids are simply moved to enter the pharynx.
Pharyngeal stage - The soft palate and uvula elevates to seal the nasopharynx off to maintain pressure and prevent food from entering the nasopharynx. A period of apnea begins, where respiration is ceased for around 1 second for this process to occur. A peristaltic sequence begins from top to bottom to move the bolus. The epiglottis opens.
As seen in the image, the epiglottis is usually upright at rest to allow air to pass into the trachea. It is able to fold backwards when food enters the pharynx to cover the entrance to the trachea. Interestingly, the epiglottis has two surfaces - the lingual and laryngeal surface. The lingual surface has taste buds and mucosa from the oral cavity whereas the laryngeal surface has the same epithelial cells as the rest of the respiratory system. It contains cells from both systems.
The depression seen in the resting position of the epiglottis is known as the epiglottic vellecula. It is able to trap saliva during sleep so the swallowing reflex isn't constantly triggered.
The gag reflex is the involuntary reflex of the contraction of a pharyngeal muscle. This is to prevent swallowing of foreign objects and choking. It is caused by the stimulation of pharyngeal wall, base of tongue, or tonsillar area, which is somatonic stimulus. Psychogenic stimulus is another type of stimulus that is a mental trigger, usually caused by disgust.