C++ Function Overloading
C++ Function Overloading
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
Function Overloading
With function overloading, multiple functions can have the same name with
different parameters:
Example
int myFunction(int x)
float myFunction(float x)
double myFunction(double x, double y)
Consider the following example, which have two functions that add numbers of
different type:
Example
https://www.w3schools.com/cpp/cpp_function_overloading.asp 1/5
1/26/24, 10:10 PM C++ Function Overloading
int main() {
int Tutorials Exercises
myNum1 = plusFuncInt(8, Services
5); Sign Up Log in
double myNum2 = plusFuncDouble(4.3, 6.26);
HTML
CSS<< "Int:
cout JAVASCRIPT SQL
" << myNum1 PYTHON
<< "\n"; JAVA PHP HOW TO W3.CSS C
cout << "Double: " << myNum2;
return 0;
}
Try it Yourself »
Instead of defining two functions that should do the same thing, it is better to
overload one.
In the example below, we overload the plusFunc function to work for both int and
double :
Example
int plusFunc(int x, int y) {
return x + y;
}
int main() {
int myNum1 = plusFunc(8, 5);
double myNum2 = plusFunc(4.3, 6.26);
cout << "Int: " << myNum1 << "\n";
cout << "Double: " << myNum2;
return 0;
}
Try it Yourself »
Note: Multiple functions can have the same name as long as the number and/or type
of parameters are different.
https://www.w3schools.com/cpp/cpp_function_overloading.asp 2/5
1/26/24, 10:10 PM C++ Function Overloading
❮ Previous
Tutorials Exercises Services
Log in to track progress
Sign Up Log in
Next ❯
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
COLOR PICKER
https://www.w3schools.com/cpp/cpp_function_overloading.asp 3/5
1/26/24, 10:10 PM C++ Function Overloading
Tutorials
NEWSLETTER
Exercises Services
GET CERTIFIED
REPORT ERROR
Sign Up Log in
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
Top Tutorials
HTML Tutorial
CSS Tutorial
JavaScript Tutorial
How To Tutorial
SQL Tutorial
Python Tutorial
W3.CSS Tutorial
Bootstrap Tutorial
PHP Tutorial
Java Tutorial
C++ Tutorial
jQuery Tutorial
Top References
HTML Reference
CSS Reference
JavaScript Reference
SQL Reference
Python Reference
W3.CSS Reference
Bootstrap Reference
PHP Reference
HTML Colors
Java Reference
Angular Reference
jQuery Reference
FORUM ABOUT
W3Schools is optimized for learning and training. Examples might be simplified to
improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we
cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted our
terms of use, cookie and privacy policy.
https://www.w3schools.com/cpp/cpp_function_overloading.asp 4/5
1/26/24, 10:10 PM C++ Function Overloading
W3.CSS.
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
https://www.w3schools.com/cpp/cpp_function_overloading.asp 5/5