Default Arguments and Function Overloading

Default arguments are those values that are passed to the function parameter right at its declaration. The default arguments are used in situations a program is expected to repeatedly invoke a function with the same argument. In such situations a value is passed once to the functions so avoid the bother passing the same value to the function at every call. Default arguments are only placed in the declaration of a function (typically placed in a header file). The compiler must see the default value before it can use it. Consider the example below
#include<iostream> using namespace std;

int PupilAge( int MinmAge=7) //PupilAge() is declare with a default value 7

{ int PupAge= MinmAge; return PupAge; }
//end myAge() int main()
//start main() { int MaxYears=9; //Main calls PupilAge() with a default in case of the minimum age and then passes a value to when calling for a maximum age. cout<< "The minimum and maximum entry level age for a primary pupil is " << PupilAge()<< " and "<< PupilAge (MaxYears)<< " years respectively" <<endl; return 0; } //main()
The minimum and maximum entry level age for a primary pupil is 7 and 9 years respectively
Default arguments allow for the use of a single function name in different situations. The compiler substitutes arguments if they are omitted by the calling function. In the above example we called PupilAge function without passing any argument and after we passed in an argument. In both cases the compiler to determine the appropriate value to use. Important two rules apply when using default arguments. First, only rightmost arguments should be defaulted. This means that a default argument cannot be followed by a non-default argument in a function call. Secondly, once a default argument is used in function call, all the subsequent arguments in that function’s argument list must be defaulted.

Function Overloading

Function overloading allows for the same name to be defined but with different signatures. Function overloading provides for declaration using same name of functions that perform similar tasks but on different data types. The compiler is able to select the appropriate function by examining the types, number and arguments used in the call. For example a function that calculates for the area of a rectangle can do it when measurements are given either as int or double. So, instead of redefining the function we can change the data types and still use the same functions. Follow the example below
int RecArea(int w,int h)
{ return w*h; }
// only the signature changes types double RecArea(double w,double h)
{ return w*h; }
int main()
{
int width=5, height=6; double wid=12.5, heit=6.2;
cout<<" When using int, the function RecArea() returns " <<recArea(width,height)<<" and " << recArea(wid,heit) <<" when double is used in the function call.";
return 0; }
When using int, the function RecArea() returns 30 and 77.5 when double is used in the function call.
The compiler uses only the parameter list (i.e data types, number and order of arguments passed) to distinguish between the overloaded functions. It does use the return type. Thus, it is not the return type but the different data type in the parameter list that was used by the compiler in determining which function to call. Note that even though we did not change the number in the list, it is still one of the possibilities that can be used to overload a function.

PREV:Introduction to passing by value and by reference


NEXT: Introduction to function templates and recursion

1 comment:

  1. can we pass default argument in overloaded function?
    if we can passed so HOW?
    if not then why not?

    ReplyDelete