Default argument

I am using Windows10. In 1.8x I used a default argument for a function, as in:
int takeAction(int DoIt, waitime=_defaultWaitTime)
{ .... }
If I didn't pass in an argument for waitime, then the default was used, just as in C++.

With IDE 2.0 BETA 9, I get an error with all calls to the function that don't pass in the second argument. This was code that worked fine with V 1.8x.

What is the error ?

Your post was MOVED to its current location as it is more suitable.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

Is not valid, you need to give the second argument a type.

I did more analysis. The error I get is "Error: too few arguments to function 'int takeAction(int, int)' and it refers only to a block of code where I define the takeAction() function because the compiler complains it isn't in scope. My code is broken down into 3 tabs. The takeAction() function is in one of them. It is invoked by a function in the same tab, and there I've no problem. It was to solve a different issue that I put a call to the takeAction() function in another tab. However, I'd thought the different tabs was just for ease of programming, that everything was all at the same level. I never before had an error caused by invoking a function from one tab to another.

Getting this scope error, I declared takeAction(int, int) outside the code -- before setup and loop(), right after the includes. Because the second parameter (an int) isn't shown to be optional in this declaration, is the reason I got the error. Please note that in the function implementation I specify int, int as in takeAction(int doIt, int waitime=defaultWait);

I think this issue is a quirk that's easily solved by passing a 0 for the second parameter. I will most likely research the issue some more, but at least I have a solution. I think I need educate myself on how to declare an extern function with a default parameter.

If you give a default argument to a function in a sketch, the parser does not create a prototype declaration, so it can not compile without write prototype in myself.
It's success to compile if you write that function before actually use it.

I thought that Arduino keeps in mind that we can easily write a function without having to good knowledge about the prototype and C++.
But if you want to create a function with default arguments in C++, you need to declare the default value only in the prototype.

However, I think this goes against the Arduino principle of hiding prototype declarations from users.

int mysum(int x, int y = 20) {
  return x + y;
}
void setup() {
  Serial.begin(9600);
  Serial.println(mysum(10));
}
void loop() {
}

It can be compiled.
Because no prototype declaration required.

But

void setup() {
  Serial.begin(9600);
  Serial.println(mysum(10));
}
void loop() {
}
int mysum(int x, int y = 20) {
  return x + y;
}

It can't be compiled.
Because the parser doesn't generate a prototype declaration.

But

int mysum(int , int = 20);
void setup() {
  Serial.begin(9600);
  Serial.println(mysum(10));
}
void loop() {
}
int mysum(int x, int y) {
  return x + y;
}

It can be compiled.
Because it prototype declaration myself.

Please provide a complete minimal demonstration of the problem.

.ino file tabs are all concatenated into a single file by the sketch preprocessor before compiling. The order of concatenation is the file that matches the sketch folder name, followed by the other .ino files in lexicographical order.

Recent discussion on the subject (which even included the use of tabs, even though that is not relevant) here:
https://forum.arduino.cc/t/tabs-in-arduino-ide/881167/27

@pert

Thanks for the link.
I'll follow that topic.

Well sure, functions with default values are rarely used.
It's often used in class but...

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.