Default argument
This article needs additional citations for verification. (May 2009) |
DESCRIPTION
In computer programming, a default argument is an argument to a function that a programmer is not required to specify. In most programming languages, functions may take one or more arguments. A default argument allows us to call a function without specifying all of its arguments. In such cases, the function assigns a default value to any parameter which does not have a matching argument in the function call. Default values are specified when the function is declared. Here is an example of a prototype (i.e., function declaration) with default values:
SYNTAX
float amount(float principlal, int period, float rate = 0.5)
- Now if we call the function by taking only 2 arguments,then the third argument will get pass automatically (by Default).
let,the variable be called 'value'.
value = amount (564.54, 6) Then it passes the the value 564.54 to the principle and the value 6 to the period and then lets the function use default value 0.5 for rate.
Default arguments are frequently used in serious programming for Ex bank interest may remain the same for all customers for perticular period of deposit. It also provide greater flexibility to the programers.Using Default arguments, a programmer can use only those arguments that are required for perticular situation.
Default arguments are formal parameters that are given a value (the default value) to use when the caller does not supply an actual parameter value. Of course, actual parameter values, when given, override the default values.
The syntax and and utility of default arguments is illustrated by the following example based on the Frame class.
class Frame { private:
...
public:
... void Resize (int width = 100, int height = 150); ...
};
In this example, the width and height arguments are both given default values. To see the effect of these default values consider the following use of the Resize method:
Frame window (100, 100, 300, 400);
...
window.Resize(200, 250); // case 1 window.Resize(200); // case 2 window.Resize(); // case 3
In the first case the window is resized to be 200 X 250: since both parameters are given the default values are ignored. In the second case, the Frame object is resized to be 200 X 150: the default value of 150 is used for the absent second (height) argument while the supplied 200 is used for the first (width) argument. In the third case, the window is resized to be 100 X 200 since both default values are used.
Default values can also be used with constructors. For example the Frame class could be redefined as follows:
class Frame { private:
...
public: Frame( char *name, int initXcoord = 50, int initYcoord = 50, int initWidth = 100, int initHeight = 100 );
...
};
This single constructor can be used in any of the following ways:
Frame display1("Fully Specified", 200, 200, 300, 350);
Frame display2("No Height", 200, 200, 300); Frame display3("Position Only", 200, 200); Frame display4("No YCoordinate", 200) Frame display5("Name Only");
The Frame display1 is given a name and will be located at position (200,200) with a shape of 300 by 350. The Frame display2 is like display1 except that it has a default height of 100 as specified by the default value for initHeight. The Frame display3 is given a name and will be located at position (200,200) and it will have a shape of 100 by 100 as determined by the default values for initWidth and initHeight. The Frame display 4 is similar to display3 except that it has a different name and will be located at (200,50), the y coordinate of 50 being determined by the default value for the parameter initYcoord. Finally, display5 will have the name indicated and the default values will determine its position - (50,50) - and it shape - 100 by 100.
Notice that as with overloaded methods, the use of default parameters give the caller of a method added flexibility in how to invoke the method. The caller need only specify those parameters that are essential and allow the default values to apply for all other parameters. Callers who need more exact control of the methods arguments can override the default values by providing their own values.
Default values can be used when:
a default value represents a commonly occurring value,
a "safe" value guaranteeing that a sensible computation will result, or
an "unreasonable" (e.g., out of range) value that causes the method to use some algorithm to compensate for the the missing argument value.
Examples of common or safe default values are an integer "counter" whose default initial value is zero or a text value whose initial contents is blank or null. A variation of the Frame class is an example of using unreasonable default values. In this variation the default values for the x coordinate, the y coordinate, the height, and the width are -1, clearly unreasonable values. The Frame constructor when confronted with (some of) these default values uses an internal algorithm to determine what values to use for each defaulted argument. The algorithm may, for example, attempt to minimize the overlap of this window with other windows on the screen.
The rule for specifying default values is that a argument without a default value can not occur after (to the right of) an argument with a default value. The following example illustrates why this rule is needed:
class Cube { public:
... Resize( int height = 10, int width = 20, int depth); ...
};
Notice that this violates the rule for default arguments because the argument depth has no default and occurs after width which has a default value. Consider the following code that uses the Resize method:
Cube cube; ... cube.Resize(30, 50);
This invocation of Resize could reasonably mean any one of three things:
cube.Resize(10, 30, 50); // uses height default cube.Resize(30, 20, 50); // uses width default cube.Resize(30, 50); // ERROR - no depth argument
This ambiguous situation is avoided (both for the benefit of the compiler and the programmer) by the rule given above.
Usually, each argument must be specified in full (this is the case in the C programming language[1]).
Later languages (for example, in C++) allow the programmer to specify default arguments that always have a value, even if one is not specified when calling the function. For example, in the following function declaration:
int my_func(int a, int b, int c=12);
This function takes three arguments, of which the last one has a default of twelve. The programmer may call this function in two ways:
result = my_func(1, 2, 3);
result = my_func(1, 2);
In the first case the value for the argument called c is specified as normal. In the second one, the argument is omitted, and the default value of 12 will be used instead.
There is no means to know if the argument has been specified by the caller or if the default value was used.
The above mentioned methode is especially usefull when one wants to set default criteria so that the function can be called with or without paramaters. Condsider the following:
void printToScreen(istream &input = cin)
{
// this outputs any input to the screen
cout << input;
}
The function call:
printToScreen();
will by default print input form the keybord to the screen. As this is the most commonly used behaviour it makes sense not to specify
printToScreen(cin);
on the other hand any paramater of type istream can now be passed to the same function and the function will print to screen from the source specified as the paramater to the function. Consider:
printToScreen(fileName);
where fileName is a file that has been opened to read via ifstream's open function call.
Some other languages, like Java, do not have default arguments. However, the same behavior can be simulated by using method overloading to create overloaded methods of the same name, which take different numbers of arguments; and the versions with fewer arguments simply call the versions with more arguments, with the default arguments as the missing arguments:
int MyFunc(int a, int b) { return MyFunc(a, b, 12); }
int MyFunc(int a, int b, int c) { /* main implementation here */ }
References