/    /  CPP – Default Arguments

Default Arguments

 

A default argument is a value given in the function statement which is automatically associated to default value by the compiler if the function call does not provide a value for the argument.

 

Example Program:

 

// A function with default arguments can be called with two arguments or three arguments or four arguments.

#include<iostream>
using namespace std;
int add(int x, int y, int z=0, int w=0)
{
return (x + y + z + w);
}
/* Drier program to test above function*/
int main() {
cout << add(10, 15) << endl;
cout << add(10, 15, 25) << endl;
cout << add(10, 15, 25, 30) << endl;
return 0;
}

 

Output: