default arguments

does the Arduino version of C not allow the use of default arguments for functions?

I got the following error when trying to use one:

In function 'void moveServo(int, int, int)': error: default argument given for parameter 3 of 'void moveServo(int, int, int)' In function 'void moveServo(int, int, int)':

Here's the function:

void moveServo(int ServoPin, int PulseWidth, int time=0) {
  Serial.print(" #");
  Serial.print(ServoPin); //which servo to move
  Serial.print(" P ");
  Serial.print(PulseWidth); // the pulse width to send

  if (time != 0) {
    Serial.print(" T "); //temp command (time = 1 second)
    Serial.print(MoveTime);

  }
}

The arduino build process has a transformation step where prototypes are created and added to the top of the sketch. I would guess that the prototype building prevents default arguments

damn oh well...

What exactly do you mean by prototyping? It builds a sort of barebones framework (of functions and their calls and such) before it compiles the rest of the code?

the build process puts function declarations (aka prototypes) at the top of the sketch. The parser that scans the sketch does not understand c++ so can't make sense of the default arguments. at least, thats what i suspect is happening.

You can get more detail on the build process here: http://www.arduino.cc/en/Hacking/BuildProcess

Hi, i was wondering about default arguments as well. Thanks for the explanation mem!
To explain what prototypes are and what they’re useful for (i had no idea about the latter):
Excerpt from the wikipedia http://en.wikipedia.org/wiki/Function_prototype:

#include <stdio.h>
 
 /* 
  * If this prototype is provided, the compiler will catch the error 
  * in main(). If it is omitted, then the error will go unnoticed.
  */
 int fac(int n);              /* Prototype */
 
 int main() {                 /* Calling function */
     printf("%d\n", fac());   /* ERROR: fac is missing an argument! */
     return 0;
 }
 
 int fac(int n) {             /* Called function  */
     if (n == 0) {
         return 1;
     }
     else {
         return n * fac(n - 1);
     }
 }

The function “fac” expects an integer argument to be on the stack when it is called. If the prototype is omitted, the compiler will have no way of enforcing this and “fac” will end up operating on some other datum on the stack (possibly a return address or the value of a variable that is currently not in scope). By including the function prototype, you inform the compiler that the function “fac” takes one integer argument and you enable the compiler to catch these kinds of errors.

www.arduino.cc/en/Hacking/BuildProcess says the following under “Transformations to the main sketch file”:

… Next, the environment searches for function definitions within your main sketch file and prepends declarations (prototypes) for them to the top of your sketch. Note that these prototypes will appear before any type declarations or #include statements in your code, meaning that they cannot contain references to custom types.

It’s unclear to me what the IDE does in case there is already a prototype for a function. I’ve no board here to test, but maybe you can define default arguments in a prototype on your own. that is, if the IDE does not overwrite them…something like this

void moveServo(int ServoPin, int PulseWidth, int time=0); //prototype
//...
void moveServe(int ServoPin, int PulseWidth, int time){
Serial.print(" #");
  Serial.print(ServoPin); //which servo to move
  Serial.print(" P ");
  Serial.print(PulseWidth); // the pulse width to send

  if (time != 0) {
    Serial.print(" T "); //temp command (time = 1 second)
    Serial.print(MoveTime);

  }

}

kuk

The parser that scans the sketch does not understand c++ so can’t make sense of the default arguments.

It actually doesn’t understand C either, which is also the problem. Well, that and it’s using regular expressions and is not a “true” parser which is why it’s causing problems.

I also don’t think it’s smart enough to recognise existing function prototypes in a sketch.

–Phil.

P.S. Oh, although now that I think of it, if it doesn’t recognise it as a function declaration then you might be able to supply your own prototype and not have a clash.

well I guess that solves that... ;)

any chance default arguments will be supported any time soon?

well I guess that solves that… :wink:

Actually, it turns out it may not after all–I think we were falsely maligning the regexp parser in this case. :slight_smile:

From reading this thread I think you’re not using the default argument syntax correctly. It seems the default argument must be in the function prototype not the function definition.

any chance default arguments will be supported any time soon?

Seems like they might be already, try this:

// Put this somewhere near the top:
void moveServo(int ServoPin, int PulseWidth, int time=0);

// Change existing function definition to this:
void moveServo(int ServoPin, int PulseWidth, int time) {
...
}

For the record, this compiled in the IDE for me–I didn’t run it though:

int foo(int i=42);

int foo(int i) {
}

void setup() {
  foo();
}

void loop() {
}

–Phil.

P.S. In this case searching for the error message fragment “default argument given for parameter” would have given you the answer. :slight_smile:

1 Like

hey all, tested it on a board and it works! as suggested define default arguments in the function prototype instead of definition.

code:

int blink(int i=1); //"blink" prototype (tested for i=0 and i=1 )


void setup() {
  //blink once to say hello
  digitalWrite(13,1);
  delay(500);
  digitalWrite(13,0);
  delay(500);
}

void loop() {
  blink(); // "blink" called without an argument
} 

int blink(int i) { //"blink" definition
  digitalWrite(13,i);
  delay(500);
  digitalWrite(13,0);
  delay(500);
}

kuk

thanks guys, works great!

by the way... The function is for communicating with the lynxmotion SSC-32 servo controller (http://www.lynxmotion.com/Product.aspx?productID=395) over a standard serial connection...

The servo controller is great, and it's open source to boot! can't reccomend it highly enough

PS - no I am not affiliated with lynxmotion in anyway, nor do I get kickback if you click that link or buy anything... just trying to let people know what works well, and easily for me