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?

8th Oct 2017, 6:13 AM
Mehdi
Mehdi - avatar
3 Answers
+ 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; }
8th Oct 2017, 6:39 AM
Babak
Babak - avatar
+ 11
Functions having same name but different arguments type this is called function overloading a basic example : https://code.sololearn.com/cz1d5fEJ18X4/?ref=app
8th Oct 2017, 6:20 AM
P R
P R - avatar
+ 2
overload functions
8th Oct 2017, 6:16 AM
Daniel
Daniel - avatar