A virtual function is a member function declared in a base class and redefined (overridden) by a derived class. When referencing a derived class object using a pointer or reference to the base class, calling a virtual function executes the derived class’s version. This mechanism ensures the correct function is called regardless of the reference type and is key to achieving runtime polymorphism. Virtual functions are declared with the virtual keyword in the base class, and their resolution occurs dynamically at runtime. While it is common for derived classes to override virtual functions, it is not mandatory; in such cases, the base class version is used.