Advertisement

Default Parameter Values!!!

Started by December 13, 2000 06:06 PM
0 comments, last by saisoft 24 years, 1 month ago
I was struggling on this a little!!! I have a function: int add(int a, int b = 5) { return (a + b); } If i put this function above main and then call add(5) in main, it compiles fine. If I put this function below main without the prototype, int add(int,int), it says add is unidentified. If i put the prototype and call add(5), it says add does not take 1 parameter. I thought you could declare functions anywhere in c++ and why does it give me errors.
You need to put the default parameter as part of the function declaration, not the function definition. So if you use the declare the function before main, and define the function after main, you need to something like:

int add(int a, int b = 5);int main(int argc, char ** argv) {  // blah}int add(int a, int b) {  // blah} 

This topic is closed to new replies.

Advertisement