Default Arguments in C++ Function
On this page (5sections)
Definition
The default argument is an assigned value in the function declaration. That value will be assigned by the compiler when function call doesn’t have that argument.
Syntax
return_type function_name( param_1,param_2 = value )
for example,
int fn_multipy(int x, int y = 1);
Default Arguments in Function Example Program
//Simple Default Argument Function Example Program in C++
//Function Example
#include<iostream>
#include<conio.h>
using namespace std;
int fn_multipy(int x, int y = 1);
int main() {
int a = 200, b = 100;
cout << "Simple Default Argument Function Example Program\n";
cout << "\nWorks for a and b :" << fn_multipy(a, b);
cout << "\nWorks for a :" << fn_multipy(a);
getch();
}
// Default Argument Function
int fn_multipy(int x, int y) {
return (x * y);
}
Sample Output
Simple Default Argument Function Example Program
Works for a and b :20000
Works for a :200
Related Pages
Continue learning with these related tutorials and programs:
- C++ Tutorials — Browse all C++ Tutorials.
- C++ Function Basics — Tutorial — function syntax and calling conventions.
- Function Prototyping In C++ — More in functions in c.
- Function Types in C++ — More in functions in c.