C++ | Function Overloading

We have learned about functions in the previous article now let’s see function overloading another feature of C++. In this article, we will be learning about function overloading and how it actually works.

Function Overloading

Function overloading is a feature of object-oriented programming where two or more functions can have the same name but different types of parameters or a different number of parameters. When a function name is overloaded with different types of jobs it is called as Function Overloading.

In Function Overloading the name of the function has to be the same and the arguments should be different. Function overloading is a very good example of static polymorphism one of the features of C++. ( Never heard of the term polymorphism? No worries we will learn about it in the coming articles)

Types of Function Overloading

Function overloading doesn’t really have any types but can be classified into the following:

  1. Functions with the same name and different types of parameters.
  2. Functions with the same name and have a different number of parameters.
  3. Functions with the same name and have a different sequence of parameters.

Different Types of Parameters

Example:

Output:

sum = 12
sum = 11.5

Different Number of Parameters

Example:

Output:

sum = 12
sum = 15

Different Sequence of Parameters

Example:

Output:

sum = 12.5
sum = 11.5

Example for Function Overloading:

Output:

 Here is int 10  
 Here is float 10.1
 Here is char* ten

How does Function Overloading work?

We have seen what Function overloading is and what are the different types of function overloading now let’s see how all these work, that is how the compiler implements this function overloading internally.

  • First, the compiler sees the function call and finds the Exact match (Function name and Parameter)
  • If no match is found:
    -> char unsigned char, and short are promoted to int.
    -> Float is promoted to double.
  • If no match is found even then:
    ->C++ tries to find a match through the standard conversion.
  • Else an ERROR will be thrown.
See also  High-Level vs Low-Level Language

That is all for this article, hope you understood the concept well. For any queries comment down your questions, and we will try to respond ASAP!

From the next article, we will learn about the oops concepts!!!

Happy Coding!!

Leave a Comment

Your email address will not be published. Required fields are marked *

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

we provide projects, courses, and other stuff for free. in order for running we use Google ads to make revenue. please disable adblocker to support us.