Explain the function of a diode.
A diode is a semiconductor device that allows current to flow in one direction only. It has two terminals, an anode and a cathode. Diodes are commonly used for rectification (converting AC to DC), in voltage regulation, and as protection devices in circuits.