Declare of function default value
Hello,
This is a simple question, however I can't find the answer on the net.
I have this code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
#include <iostream>
using namespace std;
void myFunc(int x = 3)
{
cout << x << endl;
}
int main()
{
myFunc();
return 0;
}
|
which works fine. Now I want to declare the function on top so that I move the function definition to the bottom of the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
#include <iostream>
using namespace std;
void myFunc(int x);
int main()
{
myFunc();
return 0;
}
void myFunc(int x = 3)
{
cout << x << endl;
}
|
Because of the default value, my compiler doesn't accept it. I tried many syntaxes, all failed. Any idea?
On line 8 the default value is not visible ( because you expressed it on line 13 )
This is the correct way:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
void myFunc(int x = 3);
int main()
{
myFunc();
return 0;
}
void myFunc(int x)
{
cout << x << endl;
}
|
ah the closest try I had was to put (int x = 3) for both declare and definition.
Thanks a lot! :)
Topic archived. No new replies allowed.