×

C++ Programs

C++ Most popular & Searched Programs

C++ Basic I/O Programs

C++ Constructor & Destructor Programs

C++ Manipulators Programs

C++ Inheritance Programs

C++ Operator Overloading Programs

C++ File Handling Programs

C++ Bit Manipulation Programs

C++ Classes & Object 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

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.