• The same function name is used for more than one function definition in a particular module, class or namespace • The functions must have different
type signatures, i.e. differ in the number or the types of their formal parameters (as in C++) or additionally in their return type (as in Ada). Function overloading is usually associated with
statically-typed programming languages that enforce
type checking in
function calls. An overloaded function is a set of different functions that are callable with 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. Function overloading differs from forms of
polymorphism where the choice is made at runtime, e.g. through
virtual functions, instead of statically.
Example: Function overloading in C++ import std; // Volume of a cube. int volume(int s) { return s * s * s; } // Volume of a cylinder. double volume(double r, int h) { return std::numbers::pi * r * r * static_cast(h); } // Volume of a cuboid (rectangular prism). long volume(long l, int b, int h) { return l * b * h; } int main() { std::println("{}", volume(10)); std::println("{}", volume(2.5, 8)); std::println("{}", 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==