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