English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Two or more functions with the same name but different parameters are called overloaded functions. In this article, you will learn about function overloading through examples.
Functionis a code snippet that groups code to perform a specific task.
In C ++In programming, if the number or type of parameters passed is different, two functions can have the same name.
Functions with different numbers or types (or both) of parameters are called overloaded functions. For example:
int test() {} int test(int a) {} float test(double a) {} int test(int a, double b) {}
Here, all4These functions are overloaded functions because the parameters passed to these functions are different.
Note that this4The return types of these functions are not the same. Overloaded functions may or may not have different return types, but they should have different parameters.
// Error code int test(int a) {} double test(int b){}
Even though the return types are different, the number and data types of the parameters passed to these two functions are the same. Therefore, the compiler will throw an error.
#include <iostream> using namespace std; void display(int); void display(float); void display(int, float); int main() { int a = 5; float b = 5.5; display(a); display(b); display(a, b); return 0; } void display(int var) { cout << "Integers: " << var << endl; } void display(float var) { cout << "Floating-point numbers: " << var << endl; } void display(int var1, float var2) { cout << "Integers: " << var1; cout << " And floating-point numbers: " << var2; }
output result
Integers: 5 Floating-point numbers: 5.5 Integers: 5 And floating-point numbers:5.5
Here, the display() function is called three times with different types or numbers of parameters.
All these functions have the same return type, but this is not necessary.
//The program calculates the absolute value //Valid for both integers and floating-point numbers #include <iostream> using namespace std; int absolute(int); float absolute(float); int main() { int a = -5; float b = 5.5; cout << "number " << a << " absolute value= " << absolute(a) << endl; cout << "number " << b << " absolute value= " << absolute(b); return 0; } int absolute(int var) { if (var < 0) var = -var; return var; } float absolute(float var) { if (var < 0.0) var = -var; return var; }
output result
number -5 absolute value= 5 number 5.5 absolute value= 5.5
In the above example, two absolute() functions are overloaded.
These two functions both take a single parameter. However, one function takes an integer as a parameter, while the other takes a float as a parameter.
When calling the absolute() function with int as the parameter, this function will be called:
int absolute(int var) { if (var < 0) var = -var; return var; }
When calling the absolute() function with float as the parameter, this function will be called:
float absolute(float var) { if (var < 0.0) var = -var; return var; }