...

/

Function Overloading and Overload Resolution

Function Overloading and Overload Resolution

Learn about function overloading and the overload resolution in D.

We'll cover the following...

Function overloading

Defining more than one function that have the same name is function overloading. In order to be able to differentiate these functions, their parameters must be different.

The following code has multiple overloads of the info() function, each taking a different type of parameter:

Press + to interact
import std.stdio;
void info(double number) {
writeln("Floating point: ", number);
}
void info(int number) {
writeln("Integer : ", number);
}
void info(string str) {
writeln("String : ", str);
}
void main() {
info(1.2);
info(3);
info("hello");
}

Although all of the functions are named info(), the compiler picks the one that matches the argument that is used when making the call. For example, because the literal 1.2 is of type ...

svg viewer