Home » Learn C++ » Default Arguments

Default Arguments

Default Arguments:

            The arguments in which data is initialized during the function declaration arc called default arguments. If the values of the arguments are specified in the function declaration then the arguments of the function in the function call can be omitted. If the arguments are omitted in function call then the default values are automatically passed to the function definition.

            For example, in the following program two default parameters are defined in the function declaration:

 

Program:

#include <iostream.h>
#include <conio h>
main ( )
{
void temp (char [ ] = “Canada”,
int = 2);
clrscr ( );
temp( );
temp ( “Toronto”, 10);
cout << “Ok”;
}
void temp (char x [15], int y)
{
for(int c=1; c<=y; c++)
cout << x << endl;
}

 

In the above program, the function “temp” has two default parameters, first of string type and second of integer type. These values are initialized at the time of function declaration. When the function “temp” is called without using parameter values, the default parameter values are passed to the function definition and the string “Canada” will be printed twice.

            In the second call, the parameter values “Toronto” and “10” are also passed with the function call. In this case, the string “Toronto” will he printed ten times.

Other Topics

comment closed

Get Adobe Flash playerPlugin by wpburn.com wordpress themes
Copyright © 2010 Information Village. All rights reserved.