What is the use/advantage of function overloading?

IMO, the primary benefit is consistency in the naming of methods / functions which logically perform very similar tasks, and differ slightly in by accepting different parameters. This allows the same method name to be reused across multiple implementations.

e.g. The overloads: (Good)

function Person[] FindPersons(string nameOfPerson) { ... }
function Person[] FindPersons(date dateOfBirth) { ... }
function Person[] FindPersons(int age, string dogsName) { ... }

Are preferable to ‘uniquely named’ functions: (Worse)

function Person[] FindPersonsByName(string nameOfPerson) { ... }
function Person[] FindPersonsByDOB(date dateOfBirth) { ... }
function Person[] FindPersonsByAgeAndDogsName(int age, string dogsName) { ... }

This way the coder writing a client which calls / consumes these functions can operate at a higher level of conceptual thinking (“I need to find a person”) and doesn’t need to remember / locate a contrived function name.

With static typing, the compiler will be left to match the applicable overload based on the usage parameters. For dynamic typing, this same match up will happen at run time, possibly resulting in failure if no appropriate match is found.

Leave a Comment