Defult Arguments
One of the most useful facilities available in C++ is the
facility to defined default argument values for functions. In the function
prototype declaration, the default values are given. Whenever a call is made to
a function without specifying an argument, the program will automatically
assign values to the parameters from the default function prototype
declaration. Fault arguments facilitate easy development and maintenance of
program.
For example, The
following program segment illustrates the default argument declaration:
#include <iostream.h>
void sum (int x = 10, int y = 20);
/ / function prototype declaration
void main ( ) / / with default argument list
{
int a,b;
sum ( ); / / function calling
}
void sum (int a1, int a2) / / function
definition
{
int temp;
temp = a1+a2; / / a1 = 10 and a2 = 20 by
default arguments
}
PROGRAM
A program find the sum of the given numbers using default
argument declaration.
/ /default argument declaration
#include <iostream.h>
void sum ( int a, int b, int c= 6, int d =
10);
void main ( )
{
int a, b, c, d;
cout << “ enter any two numbers
\n”;
cin >> a >>b;
sum (a, b) ; / / sum of default values
}
void sum (int a1, int a2, int a3, int a4)
{
int temp;
temp = a1 + a2 + a3 + a4;
cout << “ a = “ << a1
<< endl;
cout << “ b = “ << a2
<< endl;
cout << “ c = “ << a3
<< endl;
cout << “ d = “ << a4
<< endl;
cout << “ sum = “ << temp;
}
Output of the above
program
enter any two numbers
11
21
a = 11
b = 21
c = 6
d = 10
sum = 48
The above program can be slightly
modified, invoking the function sum ( )
with user defined input data as parameters. By this, the default values are not
assigned to the function sum ( ).
/ / default argument declaration
#include <iostream.h>
void main ( )
{
void sum (int a1, int a2, int a3, int
a4)
int a, b, c, d;
cout << “ enter four numbers \n”;
cin >> a >> b >> c
>> d;
sum (a, b, c, d);
}
void sum (int a1, int a2, int a3, int a4)
{
int temp;
temp = a1 + a2 + a3 + a4;
cout << “ a = “ << a1
<< endl;
cout << “ b = “ << a2
<< endl;
cout << “ c = “ << a3
<< endl;
cout << “ d = “ << a4
<< endl;
cout << “ sum = “ << temp;
}
Output of the above
program
enter four numbers
1
2 3 4
a = 1
b = 2
c = 3
d = 4
sum = 10
The function call without arguments is valid
in C++. The default arguments are given
only in the function prototypes and should not be repeated in the
function definition. The following program calculates the sum of the default
values when the function is called without arguments.
/ / default
argument declaration
#include <iostream.h>
void sum (int a = 2, int b = 4, int d = 10);
void main ( )
{
int a, b, c, d;
sum ( ); / / sum of default values
}
void sum (int a1, int a2, int a3, int a4)
{
int temp;
temp = a1 + a2 + a3 + a4;
cout << “ a = “ << a1
<< endl;
cout << “ b = “ << a2
<< endl;
cout << “ c = “ << a3
<< endl;
cout << “ d = “ << a4
<< endl;
cout << “ sum = “ << temp;
}
Output of the above
program
a = 2
b = 4
c = 6
d = 10
sum = 22
A few special cases of the function prototypes
with default arguments and invoking a function are illustrated below.
Case 1
/ /default
argument declaration
#include <iostream.h>
void main ( )
{
void sum (int a=2, int b, int c= 6,
int d = 10);
sum (b); / / invalid
}
Case 2
/ /default argument declaration
#include <iostream.h>
void main ( )
{
void main (int a=2 , int b =3 , int c ,
int d);
sum (c,d); / / invalid
}
A function may have more then one
default parameter. The default parameters must be grouped consecutively and are
available only at the end of a function declaration. Following is a valid way
of a function declaration and calling a function with default arguments.
Case 3
/ /default argument declaration
#include <iostream.h>
void main ( )
{
void sum (int a ,int b ,int c= 5 , int
d = 8);
sum (a,b) ; / / valid
}
excellent...undersatnding....ByRashmikanta Garanayak
ReplyDeleteNice. . .
ReplyDeleteThank you for explaining it in a clear manner......
ReplyDeleteWhat are default arguments
ReplyDelete