I've just started going through a beginners book of C++. I have some java experience (but having said that, I've never used default arguments in java to be honest)
So, as mentioned, my issue is with default arguments..
This is the code snippet I'm using:
#include <iostream>
using namespace std;
//add declaration
int add(int a, int b);
int main (void)
{
int number1;
cout << "Enter the first value to be summed: ";
cin >> number1;
cout << "\nThe sum is: " << add(number1) << endl;
}
int add(int a=10, int b=5)
{
return a+b;
}
The response I get from g++ compiler is: "too few arguments to function 'int add(int, int)'
Am I doing this wrong? (I've also tried it with literal arguments)
P.S. I can't seem to get the code snippet to display properly? Has the system changed?
It's the other way around
//add declaration
int add(int a=10, int b=5);
int add(int a, int b)
{
return a+b;
}
§ 8.3.6 Default arguments A default argument shall be specified only in the parameter-declaration-clause of a function declaration or in a template-parameter. A default argument shall not be specified for a parameter pack.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With