#include <iostream>
usingnamespace std;
int defaultParam (int x = 30, int y = 20, int z = 10);
int staticVar();
int main ()
{
int n = defaultParam (1,2,3);
int m = defaultParam (4,5);
cout << n << endl;
cout << m << endl;
cout << staticVar() << endl;
cout << (n>>2) << endl;
cout << ((n|m)<<2) << endl;
return 0;
}
int defaultParam (int x, int y, int z)
{
return x + y + z + staticVar();
}
int staticVar()
{
staticint x =2;
x+=2;
return x;
}
Wrong. Default parameters need to appear in either the definition or the declaration, but not both.
Ok my mistake (although I did check my book to make sure you were correct... yes I doubted Helios!)
It does say though that:
" When creating a function that has default argument values, the default values must be specified only once, and this must happen the first time the function is declared within the file. " ... and then goes on to say the compiler will display an error if you try to do so in both prototype and definition.
Line 10: function is called with arguments, 4 and 5. Variable x is assigned 4 as it appears first, variable y is assigned 5 as it appears second, variable z is assigned 10 as no given value is specified.
x + y + z + staticVar()
4 + 5 + 10 + (2+2) = 23
Line 14: staticVar() contains a static variable which is initially assigned a value of 2. It is then gievn the addition of 2 and returned. By the time the program reached line 14, staticVar() has been called twice - line 14 being third. Therefore: