Home »
C++ programs »
C++ basic programs
C++ program to demonstrate example of Default Argument
Learn about the default arguments in C++ function, how to declare and use them?
[Last updated : March 01, 2023]
C++ Default Arguments
Default argument is an important feature in c++, using default argument you can pass any argument default and can skip that argument while calling, in this example we will learn use of default argument.
Default Argument Example in C++
/*C++ program to demonstrate example of Default Argument.*/
#include <iostream>
using namespace std;
//Default argument must be trailer.
int sum(int x, int y=10, int z=20)
{
return (x+y+z);
}
int main()
{
cout << "Sum is : " << sum(5) << endl;
cout << "Sum is : " << sum(5,15) << endl;
cout << "Sum is : " << sum(5,15,25) << endl;
return 0;
}
Output
Sum is : 35
Sum is : 40
Sum is : 45