In C++ a default argument is a value provided during function declaration that is automatically assigned by the compiler only if the caller of the function fails to provide a value for the argument with a default value.
The example below is a simple C++ program that will demonstrate to use of default arguments. However, you don’t have to write 3 sum functions, rather only one function works by using default values for 3rd and 4th arguments.
Example:
#include<iostream>
// A function with default arguments, it can be called with
// 2 arguments or 3 arguments or 4 arguments.
int sum(int x, int y, int z=0, int w=0)
{
return (x + y + z + w);
}
/* Drier program to test above function*/
void main()
{
cout << sum(10, 15) << endl;
cout << sum(10, 15, 25) << endl;
cout << sum(10, 15, 25, 30) << endl;
getch();
}