0
function
if 2 functions, their argument differ, are the both functions, differ too? like: void smt(int a); and void smt(float a); are they going to be different functions?
3 odpowiedzi
+ 14
That's right. Function overloading is a way to use the same function (name and return type) with different types of argument, otherwise, you have to declare separate functions to do the same thing with different names, and that's a bit cumbersome for code readers.
Example
// overloaded prototypes
void fool();
void fool(int a, int b);
void fool(int a, float b);
void fool(int a, double b);
// Without using overloading
void fool_0();
void fool_1(int a, int b);
void fool_2(int a, float b);
void fool_3(int a, double b);
Note: The definition part is apart from each other for both cases.
void fool(int a, int b) {
cout << a + b;
}
void fool(int a, float b) {
cout << a + b;
}
void fool(int a, double b) {
cout << a + b;
}
+ 11
Functions having same name but different arguments type this is called function overloading a basic example :
https://code.sololearn.com/cz1d5fEJ18X4/?ref=app
+ 2
overload functions