The function of a generator is to convert the mechanical energy produced by the engine into electrical energy through the principle of electromagnetic induction, thereby delivering a stable and reliable power supply to various electrical equipment and systems.