In some programming languages, function overloading or method overloading is the ability to create multiple functions of the same name with different implementations. Calls to an overloaded function will run a specific implementation of that function appropriate to the context of the call, allowing one function call to perform different tasks depending on context.
For example, and are overloaded functions. To call the latter, an object must be passed as a parameter, whereas the former does not require a parameter, and is called with an empty parameter field. A common error would be to assign a default value to the object in the second function, which would result in an ambiguous call error, as the compiler wouldn't know which of the two methods to use.
Another example is a function that executes different actions based on whether it's printing text or photos. The two different functions may be overloaded as If we write the overloaded print functions for all objects our program will "print", we never have to worry about the type of the object, and the correct function call again, the call is always: .
Languages supporting overloading
Languages which support function overloading include, but are not necessarily limited to, the following:
Apex
C
C++
C#
Clojure
Swift
Fortran
Kotlin
Java
Julia
PostgreSQL and PL/SQL
Scala
TypeScript
Wolfram Language
Elixir
Nim
Crystal
Rules in function overloading
The same function name is used for more than one function definition
The functions must differ either by the arity or types of their parameters
It is a classification of static polymorphism in which a function call is resolved using some "best match" algorithm, where the particular function to call is resolved by finding the best match of the formal parameter types with the actual parameter types. The details of this algorithm vary from language to language.
Function overloading is usually associated with statically-typed programming languages that enforce type checking in function calls. An overloaded function is really just a set of different functions that happen to have the same name. For any particular call, the compiler determines which overloaded function to use, and resolves this at compile time. This is true for programming languages such as Java.
In Java, function overloading is also known as compile-time polymorphism and static polymorphism.
Function overloading should not be confused with forms of polymorphism where the choice is made at runtime, e.g. through virtual functions, instead of statically.
Example: Function overloading in C++
#include <iostream>
int Volume(int s) { // Volume of a cube. return s * s * s; }
double Volume(double r, int h) { // Volume of a cylinder. return 3.1415926 * r * r * static_cast<double>(h); }
long Volume(long l, int b, int h) { // Volume of a cuboid. return l * b * h; }
int main() {
std::cout << Volume(10); std::cout << Volume(2.5, 8); std::cout << Volume(100l, 75, 15); }
In the above example, the volume of each component is calculated using one of the three functions named "volume", with selection based on the differing number and type of actual parameters.
Constructor overloading
Constructors, used to create instances of an object, may also be overloaded in some object-oriented programming languages. Because in many languages the constructor's name is predetermined by the name of the class, it would seem that there can be only one constructor. Whenever multiple constructors are needed, they are to be implemented as overloaded functions. In C++, default constructors take no parameters, instantiating the object members with their appropriate default values, "which is normally zero for numeral fields and empty string for string fields". For example, a default constructor for a restaurant bill object written in C++ might set the tip to 15%:
Bill() : tip(0.15), // percentage total(0.0) { }
The drawback to this is that it takes two steps to change the value of the created Bill object. The following shows creation and changing the values within the main program:
Bill cafe; cafe.tip = 0.10; cafe.total = 4.00;