Default Arguments in C++

C++Server Side ProgrammingProgramming

In this tutorial, we will be discussing a program to understand default arguments in C++.

Default arguments are those which are provided to the called function in case the caller statement does provide any value for them.

Example

 Live Demo

#include<iostream>
using namespace std;
//function defined with default arguments
int sum(int x, int y, int z=0, int w=0){
   return (x + y + z + w);
}
int main(){
   cout << sum(10, 15) << endl;
   cout << sum(10, 15, 25) << endl;
   cout << sum(10, 15, 25, 30) << endl;
   return 0;
}

Output

25
50
80
raja
Published on 23-Mar-2020 08:17:58
Advertisements