Pages: [1]   Go Down
Author Topic: default arguments  (Read 1448 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 19
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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);

  }
}
Logged

London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6250
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 19
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6250
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

« Last Edit: January 22, 2008, 10:41:27 am by mem » Logged

berlin
Offline Offline
Sr. Member
****
Karma: 0
Posts: 293
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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:
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






« Last Edit: January 22, 2008, 03:21:55 pm by leKuk » Logged

New Zealand
Offline Offline
God Member
*****
Karma: 0
Posts: 999
Arduino pebbles
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
« Last Edit: January 22, 2008, 07:43:51 pm by follower » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 19
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

well I guess that solves that...  smiley-wink

any chance default arguments will be supported any time soon?
Logged

New Zealand
Offline Offline
God Member
*****
Karma: 0
Posts: 999
Arduino pebbles
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
well I guess that solves that...  smiley-wink
Actually, it turns out it may not after all--I think we were falsely maligning the regexp parser in this case. :-)

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.

Quote
any chance default arguments will be supported any time soon?
Seems like they might be already, try this:
Code:
// 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:
Code:
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. :-)
Logged

berlin
Offline Offline
Sr. Member
****
Karma: 0
Posts: 293
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

code:
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
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 19
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks guys, works great!
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 19
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Pages: [1]   Go Up
Jump to: