Default argument
![]() | This article may require copy editing for grammar, style, cohesion, tone, or spelling. (September 2011) |
This article needs additional citations for verification. (May 2009) |
In computer programming, a default argument is an argument to a function that a programmer is not required to specify. A default value is specified when the function is declared and the default value is automatically passed to the function when it is called.Default argument is a part of a function declaration and it tells compiler to pass the value for an argument if programmer deliberately misses an argument while calling the function. In the common case that many arguments are almost always the same.This saves programmer's time and effort required to remember and specify all arguments. Default arguments are useful in situations where some arguments always have the same value. For instance, bank interest may remain same for all customers for a particular period of deposit. It also provides greater flexibility to programmers.
Variation among languages
Most of the Object Oriented Programming (list of Object Oriented Programing languages) languages allow to use default arguments. Variation in the syntax is seen in these languages. Languages like C++, Python, Java... etc. are popular in the world of computer programming.
C++
C++ allows to specify default arguments. In C++ the function assigns a default value to the 'parameter' which does not have a matching argument in 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 though the programmer assigns less values of the arguments than declared at the time of function call and compiler alerts the program for possible default values.
Usage with syntax
Here is an example of a function prototype (i.e., function declaration) with a default argument:
float calcInterest(float principal, int period, float rate = 0.05)
Here rate is declared as a default parameter, with default value 0.05.
The function calcInterest is written with only two arguments but the third will get passed automatically.The compiler memorises the default argument so it knows it can still make the function call if it substitutes this third argument which is what user have told it to do by making it a default.
value = calcInterest(123.45, 6)
Here principal gets the value 123.45, period gets the value 6 and rate gets the (default) value 0.05.
calcInterest may also be called with three arguments overriding the default rate:
value = calcInterest(123.45, 6, 0.07)
Caution point
One important point to note is that only trailing arguments can have default values and therefore while declaring the function defaults must be added from right to left.
int mul (int i, int j = 5, int k = 10); // Right declaration.
int mul (int i = 5, int j); // wrong declaration.
Illustration with program
#include <iostream>
using namespace std;
int main()
{
float amount;
float value(float p, int n, float r = 0.15);
void printline(char ch = '*', int len = 40);
printline();
amount = value(5000.00, 5);
cout << "\n Final value = " << amount << "\n";
printline('=');
return 0;
}
float value(float p, int n, float r)
{
int year = 1;
float sum = p;
while(year <= n)
{
sum = sum*(1+r);
year = year+1;
}
return (sum);
}
void printline(char ch, int len)
{
for(int i = 1; i <= len; i++)
{
printf("%c", ch);
printf("\n");
}
}
Output of the above program is-
Final Value = 10056.8
Description of the program

In the above program while declaring the function value, r is initialized to 0.15 while p and n have not been initialized.So,while calling the function, values of p and n are provided and the value of r is not provided,but the default value of r i.e. 0.15 was passed automatically. If the values of p and n would have been initialized in the function value,then these values would have been override by the values provided while calling the function, the similar concept is used while calling the printline function. Here the the char '*' is override by '='.
python
In python,a function can be called with following 4 arguments.
1.keyword arguments
2.required arguments
3.variable length arguments
4.default arguments
To illustrate default arguments much clearly in python language click here.
Alternatives
In languages without default arguments (such as Java), a similar result can be achieved with method overloading. This involves declaring multiple methods with the same name and a different number of arguments. Often the implementation of the version(s) with fewer arguments is delegated to the version with more:
float calcInterest(float principal, int period, float rate) { /* main implementation here */ }
float calcInterest(float principal, int period) { return calcInterest(principal, period, 0.05) }
The method overloading approach is often more verbose and less clear than default arguments.
References
- http://people.cs.vt.edu/~kafura/cs2704/default.html
- Object Oriented Programming Language with C++.Author- E. Balagurusamy.
- Its IMP
External Links
- http://oopweb.com/CPP/Documents/ThinkingInCpp1/Volume/Chapter07.html
- http://stackoverflow.com/questions/5748264/default-arguments-in-c