If you like my posts please leave a comment.

Friday, September 28, 2012

Function with Default Arguments



In C++ we can assign default value(s) to a function’s parameter(s) which is actually useful in the occasion when matching argument is missing in the function call statement. The default values for the function parameters are specified at the time of writing function prototype. Below you can see an example of function prototype with default values. Example:
double interest(double p, int time, float rate = 0.10);
Now as you can notice that, default value is assigned in the way it is syntactically similar to variable initialization. Argument rate is assigned with a default value 0.10.
Now if the value for the argument rate is missing at the function call then this default value will be used by the function. For example, If the function call goes like this: SI = interest (7200, 3);


Here 7200 for argument p and 3 for argument time are passed but value for argument rate is missing
Now let’s see another instance. If function call goes like this:  
SI = interest (7200, 3, 0.9);
Here value for the argument rate is present so default value assigned for rate will not be used.
N
ote: Thus we have seen that, the default value of an argument can be used only when its matching value is missing in the function call statement.
            Now, let’s consider some more variations in the declaration of function interest, with one or more default arguments.
double interest(double p, int time, float rate = 0.10);//legal
double interest(double p, int time = 2, float rate);//illegal
double interest(double p = 5000, int time = 2, float rate);//illegal
double interest(double p, int time = 2, float rate = 0.10);//legal
double interest(double p = 5000, int time = 2, float rate = 0.10);// legal
From the above examples it is now very clear that, when you want to specify defaults values for one or arguments in the function declaration, you need to take care of the fact that you are assigning the default value to the argument whose right sided argument(s) is/are already been provided with a default value or itself is the right most argument in the function declaration. As, in the following case -
double interest(double p, int time, float rate = 0.10);//legal
the argument rate is itself is the right most argument so to assign a default value for rate is OK here.
But in this case - double interest(double p, int time = 2, float rate);//illegal
the argument time is neither itself the right most argument nor the argument at its right side (rate)  is assigned with some default value.
N
ote: Function with default arguments can be used in the situation where some arguments always have the same value. Also they provide greater flexibility to the programmers.
Now let’s see a simple program using function with default arguments:

#include <iostream.h>
double interest(double principal=500,int time=1,float rate=9.0);
double interest (double principal,int time,float rate)
{
     double SI;
   SI=(principal*rate*time)/100;
return (SI);
}
int main()
{
double SI;
SI=interest();
cout<<SI<<' '<<" (Here principal = 500, time = 1 and rate = 9.0)"<<endl;
SI=interest(5000);
cout<<SI<<' '<<" (Here principal = 5000, time = 1 and rate = 9.0)"<<endl;
SI=interest(5000,3);
cout<<SI<<' '<<" (Here principal = 5000, time = 3 and rate = 9.0)"<<endl;
SI=interest(5000,4,10.1);
cout<<SI<<' '<<" (Here principal = 5000, time = 4 and rate = 10.1)"<<endl;

return 0;
}
Output:

No comments:

Post a Comment