C/C++

Functions Overloading in c++ with example code

Functions Overloading:

Functions Overloading-Declaring more than one function with the same name but with a different set of arguments and return data types is called function overloading.

For example, three functions with the same name “sum” and having different parameters are declared as:

Int sum (int, int);

Int sum(int, int, int);

Double sum(double, double,  double );

The first “sum” has two parameter s both of int type and returned data type is also int type.

The second function “sum” has three parameters all of the int data types the return data type is also of int type.

The third function “sum” has three parameters, all of the double type and its return data type is also double.

When the program that has an overloaded function is compiled, the C++ compiler checks the number of parameters, their order, and data type and marks a proper function name for each function. At function call, it executes that function whose return type or parameters matched with the return type or parameter given in the function call.



Example how to use the functions overloading concept in c++ programming:

Output

In the above functions overloading program, three functions are defined with the same name “sum”. When the function “sum” is called by passing three integer values parameters, the control will shift to the 2nd function that has three integer type arguments. Similarly, when the function is called by passing three float data type values, control shifts to the 3rd function that matches the parameters.

what is Function Overloading:

Function overloading declaring multiple functions with the same name but with different set of parameters and return data types is called function overloading.

The functions with same names must differ in one of the following ways:

  • Types of parameters
  • Number of parameters
  • Sequence of parameters

When an overloaded function is called for executing C++ compiler selects the proper function by checking the number of parameters, their order and data types in the function call. The compiler marks a proper function name for each function, sometimes referred to as name decoration. In this way, a proper overloaded function is called for execution whose return type and parameters are matched with the parameters given in the function call. The compiler uses only the parameter lists to distinguish between functions of the same name.



Examples of Function Overloading:

The following program example explains the concept of function overloading:

function overloading

In this program, two function with the same name are defined before the main() function. When the function “cube’ is called by passing 3.3 value then the function ‘cube’ will be executed that has argument of double type. Similarly, when the function cube is called by passing integer value ‘5’ then the function cube that has int type argument will be executed.


Example: write a program by defining functions overloading. The name of the overloaded function is “square”. The prototypes of overloaded function are:

  • Void square(void); used to display a solid square of asterisks with side length of 4.
  • Void square(char); used to display a solid square of specified character with side length of 6.
  • Void square(char, int); used to display a solid square of specified character and with specified side length of ‘n’.

function overloading


example: how to use function overloading in c++:

In the above example, the volume of each component is calculated using one of the three functions named “volume”, with selection based on the differing number and type of actual parameters.

Rules in Functions Overloading

  • The same function name is used for more than one function definition
  • The functions must differ either by the arity or types of their parameters

It is a classification of static polymorphism in which a function call is resolved using some “best match” algorithm, where the particular function to call is resolved by finding the best match of the formal parameter types with the actual parameter types. The details of this algorithm vary from language to language.

Functions overloading is usually associated with statically-typed programming languages that enforce type checking in function calls. An overloaded function is really just a set of different functions that happen to have the same name. The determination of which functions to use for a particular call is resolved at compile time.

In Java, functions overloading is also known as compile-time polymorphism and static polymorphism.

Functions overloading should not be confused with forms of polymorphism where the choice is made at runtime, e.g. through virtual functions, instead of statically.

 

Related Articles

Leave a Reply

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

Check Also
Close
Back to top button