1. C++ Tutorials

What is function overloading in Cpp

C++ allows you to overload functions, that is, different functions can have the same

Functions in traditional programming languages, such as C, which perform the same task
but have different arguments, must have different names. To define a function that calculated the maximum value of two integers and two floating-point numbers, you would
need to program two functions with different names.

syntax of function overloading

int max( int x, int y);
double max( double x, double y);

In our example two different function share the same name, max. The function max()
was overloaded for int and double types.

The compiler uses a function’s signature to differentiate between overloaded functions.

What is Function Signatures.

A function signature comprises the number and type of parameters. When a function is
called, the compiler compares the arguments to the signature of the overloaded functions
and simply calls the appropriate function.


double maxvalue, value = 7.9;
maxvalue = max( 1.0, value);

In this case the double version of the function max() is called. When overloaded functions are called, implicit type conversion takes place.

However, this can lead to ambiguities, which in turn cause a compiler error to be issued.

Example: maxvalue = max( 1, value); // Error!

The signature does not contain the function type, since you cannot deduce the type by
calling a function. It is therefore impossible to differentiate between overloaded functions by type.

Example:      int search(string key);
            string search(string name);

Both functions have the same signature and cannot be overloaded.

Sample program of function overloading

To generate and output random numbers

#include<cstdlib>   // For rand(), srand()
#include<ctime>     // For time()

using namespace std;

bool setrand = false;
inline void init_random()     // Initializes the random
{                             // number generator with the
                              // present time.
   if( !setrand )
   { srand((unsigned int)time(NULL));
   setrand = true;

inline double myRandom()
  return (double)rand() / (double)RAND_MAX;
inline int myRandom(int start, int end)
  return (rand() % (end+1 - start) + start);

int main()
int i; 
cout << "5 random numbers between 0.0 and 1.0 :" << endl;
for( i = 0; i < 5; ++i)
cout << setw(10) << myRandom(); 
cout << endl;

cout << "\nAnd now 5 integer random numbers " "between -100 and +100 :" << endl; 

for( i = 0; i < 5; ++i)
 cout << setw(10) << myRandom(-100, +100);
 cout << endl; 
return 0; }

output In Your Screen

If you like this post, don’t forget to share 🙂

This article is written by our awesome writer
Do you like Zubeen's articles?  Follow on social!
Comments to: What is function overloading in Cpp

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

Attach images - Only PNG, JPG, JPEG and GIF are supported.

New Dark Mode Is Here

Sign In to access the new Dark Mode reading option.

Join our Newsletter

Get our monthly recap with the latest news, articles and resources.

By subscribing you agree to our Privacy Policy.

Latest Articles

Explore Tutorials By Categories


Codeverb is simply an all in one interactive learning portal, we regularly add new topics and keep improving the existing ones, if you have any suggestions, questions, bugs issue or any other queries you can simply reach us via the contact page


Welcome to Codeverb

Ready to learn something new?
Join Codeverb!

Read Smart, Save Time
    Strength indicator
    Log In | Lost Password