void Function(int p1, int p2); //Here is a simple function
void Function(int p1, int p2=5); //Here is a simple function with a default parameter
void Function(int p1, int p2=p1); //This does not work though I wish it would :(
error: local variable ‘p1’ may not appear in this context;
How do I get my function to set the 2nd parameter to the 1st parameter if a 2nd parameter was not typed in?!?!?!
But the whole function must be seen by the compiler before it's used.
So if you're just using it in one cpp file, it can be defined higher up in the same file. But -- as cpp file shouldn't be including other cpp files -- if you want to use the same function in more than one cpp file, it needs to go in a header [1].
Andy
[1] There's nothing to actually stop you cutting and pasting the identical function into loads of cpp files. This would work. But if you find an error in one occcurence, you'd have to go and edit all the others ones, too.
This tells the compiler that it can magically make all instances of the one-argument Function actually disappear from your executable (this is a good thing!) while giving you the convenience to pretend that there are actually two functions...
1 2
Function(12); // this is really compiled as: Function(12,12)
Function(19,-7);