overloading function | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

overloading function

why below code result is error?? int printName(int a) { } float printName(int b) { } double printName(int c) { }

5th Nov 2017, 5:32 PM
Amir Mehrabi
12 Answers
+ 11
Overloaded functions should have same name and different parameter list. Parameter lists may differ in three ways. 1. number of parameters (int a) (int a, int b) 2. type of parameters (int a) (double a) 3. order of parameters (int a, float b) (float a, int b) The given functions have same parameter lists, so these are not overloaded. When you call them, it'll create conflict due to same argument list.
5th Nov 2017, 5:42 PM
Shamima Yasmin
Shamima Yasmin - avatar
+ 10
that's not overloading if you call printName(15) how can the compiler know which one to pick? overloading means same name but different arguments
5th Nov 2017, 5:36 PM
Kamil
Kamil - avatar
+ 2
You have stated return types, int, float, and double, but don't have a return statement in the function, so an error occurs. Just change them to "void," and change the parameter types to different ones, and it will run fine.
5th Nov 2017, 6:24 PM
LunarCoffee
LunarCoffee - avatar
+ 2
Wait... a function with a return type specified that doesn't return anything works? OK I'm confused.
5th Nov 2017, 6:26 PM
LunarCoffee
LunarCoffee - avatar
+ 2
Oh, OK.
5th Nov 2017, 8:30 PM
LunarCoffee
LunarCoffee - avatar
+ 1
so why below code shows error ??!?! #include <iostream> using namespace std; int printName(int a) { } float printName(int b) { } double printName(int c) { } int main() { return 0; }
5th Nov 2017, 5:46 PM
Amir Mehrabi
0
could you please write code as above code and in conclusion , compile correct with no error ???
5th Nov 2017, 5:52 PM
Amir Mehrabi
0
thank you
5th Nov 2017, 6:28 PM
Amir Mehrabi
0
Oh , Yes .... that was a great hint ....
5th Nov 2017, 6:46 PM
Amir Mehrabi
0
so great thank you for more details ...
5th Nov 2017, 6:50 PM
Amir Mehrabi
0
Thait's it ... thank you for your attention
5th Nov 2017, 6:53 PM
Amir Mehrabi
0
Thank you for your reference
5th Nov 2017, 7:22 PM
Amir Mehrabi