Saturday, 14 April 2018

Default Arguments in C++ with example programs

A default argument is a value give to the function at the time of declaration, when function call is done, it automatically mapped into it by the compiler. Normally in languages like C, we pass entire parameters to function ( from calling function to called function) either by using call by value or call by reference. C++ allows us to call a function without specifying all its arguments. In such cases, the function assigns a default value to the parameter which does not have a matching argument in the function call.
Default values are specified when the function is declared. The compiler looks at the prototype to see how many arguments a function uses and alerts the program for possible default values.Generally a default argument will behave identically to an argument passed by parameter or a local variable declared at the start of the function, and have the same scope and extent (lifetime) as a parameter or other local variable, namely an automatic variable which is deallocated on function termination.

In other cases a default argument may instead be statically allocated. If the variable is mutable, it will then retain its value across function calls, as with a static variable. Here is an example program how a default argument is declared.
 int add( int x, int y, int z=10)
 int main()
cout<< add(5,6) <
cout<< add(7,8,9) <
return 0;

Key points:
  1. Default arguments are different from constant arguments as constant arguments can’t be changed whereas default arguments can be overwritten if required.
  2. Default arguments are overwritten when calling function provides values for them. For example, calling of function add(7,8,9) overwrites the value of z.
  3. During calling of function, arguments from calling function to called function are copied from left to right

Common mistakes when using Default argument

1. void add(int a, int b = 3, int c, int d = 4);

The above function will not compile. You cannot miss a default argument in between two arguments.
In this case, c should also be assigned a default value.

2. void add(int a, int b = 3, int c, int d);

The above function will not compile as well. You must provide  default values for each argument after b.In this case, c and d should also be assigned default values.

If you want a single default argument, make sure the argument is the last one. void add(int a, int b, int c, int d = 4);

When to use default arguments?

Default arguments are useful in situations where some arguments always have the same value. For example, bank interest may remain the same for all customers for a particular period of deposit. It also provides grate flexibility to programmers.
A function can be written with more parameters than are required for its most common application. Using default arguments, a programmer can use only those arguments that are meaningful to a particular situation.
Q) Does java supports default arguments feature?
  No, Java does not support to assign a function arguments as a default. But the same feature can be achieved by using method overloading. 


Post a Comment


Find Us On Facebook

C Programming


C++ Tutorial


Java Tutorial


Computer Basics


MS Office


Database Management