0% found this document useful (0 votes)
15 views

C++ Function Overloading

Uploaded by

ramukaka777787
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

C++ Function Overloading

Uploaded by

ramukaka777787
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1/26/24, 10:10 PM C++ Function Overloading

 Tutorials  Exercises  Services   Sign Up Log in

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C

C++ Function Overloading


❮ Previous Next ❯

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

int plusFuncInt(int x, int y) {


return x + y;
}

double plusFuncDouble(double x, double y) {


return x + y;
}

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;
}

double plusFunc(double x, double 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



 SPACES UPGRADE AD-FREE

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

Top Examples Get Certified


HTML Examples HTML Certificate
CSS Examples CSS Certificate
JavaScript Examples JavaScript Certificate
How To Examples Front End Certificate
SQL Examples SQL Certificate
Python Examples Python Certificate
W3.CSS Examples PHP Certificate
Bootstrap Examples jQuery Certificate
PHP Examples Java Certificate
Java Examples C++ Certificate
XML Examples C# Certificate
jQuery Examples XML Certificate

    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.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by

https://www.w3schools.com/cpp/cpp_function_overloading.asp 4/5
1/26/24, 10:10 PM C++ Function Overloading
W3.CSS.

 Tutorials  Exercises  Services   Sign Up Log in

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C

https://www.w3schools.com/cpp/cpp_function_overloading.asp 5/5

You might also like