Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
20 views

WWW W3schools Com CPP CPP - Function - Overloading Asp

The document discusses function overloading in C++. It explains that function overloading allows defining multiple functions with the same name but different parameters. This allows a single function to work for different data types. The document provides an example of overloading a function to handle both integer and double data types.

Uploaded by

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

WWW W3schools Com CPP CPP - Function - Overloading Asp

The document discusses function overloading in C++. It explains that function overloading allows defining multiple functions with the same name but different parameters. This allows a single function to work for different data types. The document provides an example of overloading a function to handle both integer and double data types.

Uploaded by

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

 Tutorials  Exercises  Services  

C#
 BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY

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;
}
int main() {
int myNum1 = plusFuncInt(8, 5);
double myNum2 = plusFuncDouble(4.3, 6.26);
cout << "Int: " << myNum1 << "\n";
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.
❮ Previous Next ❯

COLOR PICKER




SPACES
UPGRADE

NEWSLETTER

GET CERTIFIED

REPORT ERROR

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
HTML Examples
CSS Examples
JavaScript Examples
How To Examples
SQL Examples
Python Examples
W3.CSS Examples
Bootstrap Examples
PHP Examples
Java Examples
XML Examples
jQuery Examples

Get Certified
HTML Certificate
CSS Certificate
JavaScript Certificate
Front End Certificate
SQL Certificate
Python Certificate
PHP Certificate
jQuery Certificate
Java Certificate
C++ Certificate
C# Certificate
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­2023 by Refsnes Data. All Rights Reserved. W3Schools is Powered by


W3.CSS.

You might also like