Go Down

Topic: default arguments (Read 1 time) previous topic - next topic

JustAboutRealJAR

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:
Code: [Select]
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);

 }
}

mem

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

JustAboutRealJAR

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?

mem

#3
Jan 22, 2008, 04:36 pm Last Edit: Jan 22, 2008, 04:41 pm by mem Reason: 1
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


kuuk

#4
Jan 22, 2008, 09:19 pm Last Edit: Jan 22, 2008, 09:21 pm by leKuk Reason: 1
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:
Code: [Select]

#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);
    }
}

Quote

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":
Quote
... 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
Code: [Select]

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







Go Up