-->

Function Overloading


Function Overloading 

  Explain function overloading.
·  Overloading refers to the use of the same thing for different purposes or tasks.
·  To create a function that performs a variety of different tasks, is known as function overloading.
·  Using function overloading we can create or design a family of function with one function name but with different argument lists.
· The function perform different operations depending on the argument list in the function call.
·       The correct function is to be invoked is determined by checking the numbers and types of argument but not on function type
·       An overloaded sum () function.
Example:
// declarations
int sum (int a, int b); // prototype1
int sum (int a, int b, int c); // prototype2
double sum (double x, double y);// prototype3
double sum (int p, double q); // prototype4

now we can call the above function by the following statements,
total = sum(10,10);
total = sum(10,20,30);
total = sum(10.20,30.40);
total = sum(10,20.25);

1                   Write the steps of function selection in the function overloading? Or
  List out the features of function overloading (2013)
  The function selection involves the following steps:
·  The compiler first tries to find an exact match in which the types of actual argument are the same, and use that function.
·  If the perfect match is not found, the compiler uses the integral promotions to the actual arguments, such as,
§  char to int
§  float to double
to find a match.
· When perfect match and integral promotion fails, the compiler tries to use the built-in conversions (the implicit assignment conversions) to the actual arguments and then uses the function whose match is unique. If the conversion have multiple matches, then the compiler will generate an error message. Suppose we use the following two functions:
§  long squre (long n)
§  double square(double x)
§  A function call such as
§  Square(10)
·   This will cause an error because int argument can be converted to either long or double thereby creating an ambiguous situation as to which version of square() should be used.
· If all of the possible steps fail, then the compiler try to use the use-defined conversions in combination with integral promotions and built-in conversions to find a unique match. User-defined conversions are mostly used in handling class objects.