Loading....
Coupon Accepted Successfully!

 

Default Arguments

Usually, a function is called with all the arguments as declared in function prototype declaration and function definition. C++ compiler allows the programmer to assign default values in the function prototype declaration/function declarator of the function. When the function is called with less parameter or without parameters, the default values are used for the operations.

It is not allowed to assign default value to any variable, which is in between the variable list. Variables lacking default values are written first followed by the variables containing default values. This is so because C++ convention of storing variables on the stack is from right to left.

The default values can be specified in function prototype declaration or function declarator. Normally, the default values are placed in function prototype declaration. In case the function is defined before caller function, then there is no need to declare function prototype. Hence, the default arguments are placed in function declarator. The compiler checks for the default values in function prototype and function declarator and provides these values to the arguments that are omitted in function call.


The default arguments are useful while making a function call if we do not want to take effort for passing arguments that are always same. It is also useful when we update an old function by adding more arguments to it. Using default arguments, the function calls can continue to use previous arguments with the new arguments.

The following example declares the default arguments:
 


 

In this example function sum() has four arguments. They are a, b, c, and d of integer types. The variable b, c, and d are initialized with 10, 15, and 20, respectively. These values are used when the function sum() is called with fewer arguments.


7.10 Write a program to define function sum with default arguments.


Explanation: In the above example, the prototype of variable sum() is declared. The function sum() has four arguments of integer types a, b, c, and d. The variables b, c, and d are initialized with default values 10, 15, and 20, respectively, in function prototype declaration. The variable “a” is not initialized. The function sum() is called five times. Each time the result has different values.


In the first cout statement, the function sum() is called with four arguments. In this function call, no default values are used, that is actual values of variables initialized are considered. Hence, the result is 14.

In the second cout statement the function sum() is called with three arguments (one less argument). In this function call actual values of starting three variables a, b, c and default value of forth variable “d” are considered. Hence, the result is 29.

In the third cout statement, the function sum() is called with two arguments (two arguments less). In this function call actual values of starting two variables a, b and default values of last two variables c, d is considered. Hence, the result is 40.

In the fourth cout statement the function sum() is called with one argument. In this function call, the actual value of variable a and default values of variables b, c, and d, 10, 15, and 20, respectively, are taken into account. The result displayed is 47.

In the last cout statement the function sum() is called with last three arguments. The actual values of b, c, and d variables are 3, 4, and 5 and last default value 20 is together taken into account. Hence the result is 32.

7.11 Write a program to place default values in function declarator. Execute the function with default values.

Explanation: In the above example, the prototype of variable sum() is declared. The function sum() has four arguments of integer types a, b, c, and d. The variable b, c, and d are initialized with default values 10, 15, and 20, respectively, in function prototype declaration. The variable “a” is not initialized. The function sum() is called five times. Each time the result sum has different values.

In the first cout statement, the function sum() is called with four arguments. In this function call no default values are used, that is actual values of variables initialized are considered. Hence, the result is 14.

In the second cout statement the function sum() is called with three arguments (one less argument). In this function call actual values of starting three variables a, b, c and default value of forth variable “d” are considered. Hence, the result is 29.

In the third cout statement, the function sum() is called with two arguments (two arguments less). In this function call actual values of starting two variables a, b and default values of last two variables c, d are considered. Hence, the result is 40.

In the fourth cout statement the function sum() is called with one argument. In this function call the actual value of variable a and default values of variables b, c, and d are 10, 15, and 20, respectively, are taken into account. The result displayed is 47.

In the last cout statement the function sum() is called with last three arguments. The actual values of b, c, and d variables are 3, 4, and 5 and last default value 20 is together taken into account. Hence the result is 32.

7.12 Write a program to find area of triangle by using default values and actual values. The formula for area of a triangle is ½ * base * height.


Explanation: In the above program, the prototype of function area() is declared with two default values 3 and 5 for variables base and height, respectively. In function main() the variable base and height are defined and initialized with 2 and 7, respectively. In the first call of the function area() function is called with both the arguments, that is base and height. In this call no default arguments are used. In the second call the function area() is called with one argument (one less argument). In this call one actual and one default value are used. The third call is the same as second one.





Test Your Skills Now!
Take a Quiz now
Reviewer Name