+ 33
Is it allowed in c++ to have two functions with same name?
66 Antworten
+ 76
Yes, it's called function overloading.
Multiple functions are able to have the same name if you like, however MUST have different parameters.
void func(int a, int b) { }
void func(float a, int b) { }
void func(int a, int b, int c) { }
Here we have overloaded function "func", and will all work within the same program. When calling the function, the program will run the function which corresponding to the arguements you pass through. For example, if you called func(5.5, 6), it would run the second function.
+ 23
Cohen Creber described the subject completely but with a simple NOTE !!!
Be careful that when overloading a function only changing the return value with the same name and parameters is NOT an overloading, it is an ERROR.
In other word return value change in overloading doesn't count.
int func() {}
void func() {}
is an ERROR.
+ 8
Yes as long as the function in question is defined differently by its parameters. Either using a different type or different number of arguments.
This will work:
void func(int a,int b){}
void func(int a,int b, int c){}
void func(float a, float b){}
This will not:
void func(int a, int b){}
void func(int x, int y){}
+ 8
Yes,with different arguments(function overloading)
+ 6
Thanks ☺
+ 6
Yes,but want to use different parameters.
for example,
int function(int a,int b);
int function(float c,int d);
Simply,called function overloading.
+ 5
Yes ,it is oop.In every oop there is polymorphism i.e Overloading and overriding
+ 4
yes
+ 3
but different parameters list
+ 3
Yes! It is possible to use two or more function with same name in c++.
First I want to explain function overloading:
Function overloading is concept which allow to user to define two or more functions with the same name but with a different set of parameters.
Hence we can use two more function with same name using function overloading concept.
+ 3
i think she is got it so dont write too much comments on a very basic questions peoples !
+ 3
yes it is possible the functions with same name but with different parameters ...if we change the return type only it doesn't effect....so we have to change parameters or just their return types
+ 3
Yes, it is possible. It is called function overloading. Same function name but different parameters.
For example,
void sum(int a, int b) {}
void sum(float a, float b) {}
+ 3
yes
+ 3
yes, that is called function overloading,
Function overloading in C++ You can have multiple definitions for the same function name in the same scope
+ 2
yes u can have multiple functions with same name. the only condition is that these functions need to have different parameters. this feature is called function overloading.
+ 2
Yes...And It is called Function Overloading.
U can have same function name but with different parameters....!!!
+ 2
Yes, use function overloading what the other said or use template<typename T>.
+ 2
Yah its possible with a simple concept called Function Overloading!!
+ 2
yes it is possible with the concept of function overloading. where functions wit same name but different parameters or parameter count are considered to be different functions
eg. int a() & int a(int b) are different
void a(int b) & void a(float b) are different