+ 2

Default Values for Parameters

#include <iostream> using namespace std; int sum(int a, int b=42) { int result = a + b; return (result); } int main() { int x = 24; int y = 36; //calling the function with both parameters int result = sum(x, y); cout << result << endl; //calling the function without b result = sum(x); cout << result << endl; return 0; } For the 1st part we get 60, for the 2nd we get 66. In the 2nd part, 1st value was taken as input using, result = sum(x); and the 2nd value was default. What if I want to set default value for the 1st parameter and input the 2nd value? How should I input the 2nd value?

15th Nov 2016, 2:58 PM
Towfique Kabir
Towfique Kabir - avatar
1 ответ
+ 5
Default values must ALWAYS be the last parameters, but you can input them in order and then swap them around for the function.
15th Nov 2016, 3:39 PM
Keto Z
Keto Z - avatar