Class 10 Exam  >  Class 10 Notes  >  C++ Programming for Beginners  >  C++ Function Overloading

C++ Function Overloading | C++ Programming for Beginners - Class 10 PDF Download

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;

}

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;

}

Note: Multiple functions can have the same name as long as the number and/or type of parameters are different.

The document C++ Function Overloading | C++ Programming for Beginners - Class 10 is a part of the Class 10 Course C++ Programming for Beginners.
All you need of Class 10 at this link: Class 10
15 videos|20 docs|13 tests

Top Courses for Class 10

15 videos|20 docs|13 tests
Download as PDF
Explore Courses for Class 10 exam

Top Courses for Class 10

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Free

,

C++ Function Overloading | C++ Programming for Beginners - Class 10

,

Sample Paper

,

Extra Questions

,

mock tests for examination

,

Exam

,

Summary

,

Objective type Questions

,

C++ Function Overloading | C++ Programming for Beginners - Class 10

,

practice quizzes

,

study material

,

Viva Questions

,

C++ Function Overloading | C++ Programming for Beginners - Class 10

,

video lectures

,

MCQs

,

shortcuts and tricks

,

past year papers

,

Important questions

,

ppt

,

Previous Year Questions with Solutions

,

pdf

,

Semester Notes

;