Function Overloading in C++ (Using Different Parameters)

Function Overloading in C++ (Using Different Parameters)

31 Mar 2024
Intermediate
1.09K Views
9 min read
Learn via Video Course & by Doing Hands-on Labs

C++ Programming For Beginners

C++ Function Overloading: An Overview

We are now well aware of what areFunctions in C++and its various aspects like function parameters, call by value and call by reference, etc. We looked at all these in detail. In this C++ tutorial, we will see how the same function can be used for different tasks i.e. C++ function overloading and its associated issues. To go a step further, take a look at our C++ Certification Course.

What is Function Overloading in C++?

If two or more functions have the same name but different numbers and types of parameters, they are known as overloaded functions. Let us understand it by this example.
int area(int a) {} //for area of square
int area(int l, int b) { } //for area of rectangle
float test(float b, float h) { } //for area of triangle

Here, you can see that all three functions have the same name area() and they perform the same operation of calculating area. The only difference is in the number and types of parameters they require to calculate the area of different shapes. Therefore, instead of defining three functions that should do the same thing, it is better to overload one. This is known as C++ Function Overloading.

Function overloading is a compile-time polymorphism technique in C++. Therefore, it becomes an important part of the OOPs concepts in C++. It promotes code reusability and understandability.

Remember: It can also happen that multiple functions have not only the same name but also the same numbers and types of parameters. This is not function overloading but function overriding. We will look into this after understanding Inheritance in C++ in the tutorials, Polymorphism in C++, and Function Overriding in C++ respectively.

C++ Function Overloading Using Different Types of Parameters

Example


#include <iostream>
using namespace std;

// function with 2 int type parameters
int area(int x, int y) {
 return x * y;
}

// function with float type parameter
float area(float x) {
 return x * x;
}

int main() {
 int result1 = area(70, 2); //function call for the function with 2 int type parameters
 float result2 = area(5.6); //function call for the function with float type parameter
 
 cout << "Area of rectangle using integer arguments: " << result1 << endl;
 cout << "Area of square using a float argument: " << result2 << endl;
 
 return 0;
} 

The above C++ code in C++ Compiler overloads the area() function to calculate the area of a rectangle and square withdifferent types of parameters. Based on the type of arguments in the function call, the corresponding function is called.

Output

Area of rectangle using integer arguments: 140
Area of rectangle using float arguments: 47.04

Read More - C++ Interview Interview Questions for Experienced

C++ Function Overloading Using Different Number of Parameters

Example


#include <iostream> 
using namespace std;

// function with 2 float type parameters
float area(float x, float y) {
 return x * y;
}

// function with a float type single parameter
float area(float x){
 return x * x;
}

int main() {
 float result1 = area(5.6, 8.4); //function call for the function with 2 float type parameters
 float result2 = area(3.5); //function call for the function with a float type single parameter
 cout << "Area of rectangle using float arguments: " << result1 << endl;
 cout << "Area of square using a float argument: " << result2 << endl;

 return 0;
} 

The above C++ code overloads the area() function to calculate the area of a rectangle and square with the same types of parameters but having different numbers. Based on the typeand number of arguments in the function call, the corresponding function is called.

Output

Area of rectangle using integer arguments: 140
Area of rectangle using float arguments: 47.04
Area of square using a float argument: 12.25

Points to Remember

  • C++ function overloading is not only a feature of user-defined functions in C++
  • Many standard library functions in C++ are also overloaded. E.g. the sqrt() function can take double, float, int, etc. as parameters. This is possible because the sqrt() function is overloaded in C++.
  • The return type of the overloaded functions may or may not be the same.
  • The parameters for each function must be in a distinct order.
  • The parameters for the functions must be distinct.

Advantages of C++ Function Overloading

  • We can create functions with distinct purposes but with the same name by using function overloading in C++.
  • It speeds up the program's execution.
  • The code is clearer and simpler to comprehend.
  • It reduces memory utilization and makes programs reusable.

Disadvantages of C++ Function Overloading

  • It prevents the overloading of functions with various return types.
  • The identical parameters cannot be overloaded in the case of a static function.
Summary
After reading this C++ Function Overloading article, your concept of functions must have been recalled and refreshed. You just need to practice more and more to get through with the concept. Therefore, consider enrolling in our C++ Certification program.
Share Article
Batches Schedule
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 8th time in a row (2016-2023). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Self-paced Membership
  • 22+ Video Courses
  • 750+ Hands-On Labs
  • 300+ Quick Notes
  • 55+ Skill Tests
  • 45+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Accept cookies & close this