[SOLVED] Optional arguments in Function

Hi, All

I found this thread in Google : http://forum.arduino.cc/index.php?topic=49062.0

But I am making a mistake somewhere or misunderstand what is going on in that topic.

I would like to have a function with optional parameters. I tried it like so :

void setup()
{

  Serial.begin(9600);
  test4Fun(10,11);
  test4Fun(12);
}

void loop()
{
  
}


void test4Fun(int t, int t2=2)
{
  Serial.print(t);
  Serial.print(" ");
  Serial.println(t2);
}

Can someone tell me what I am doing wrong? (This is the error I am getting)

sketch_feb06a.ino: In function 'void setup()': sketch_feb06a:4: error: too few arguments to function 'void test4Fun(int, int)' sketch_feb06a:6: error: at this point in file

Optional arguments are defined in the header file or function prototype, not in the function definition.

Hi, Paul,

Thanks for the quick reply.

i know how to do this in a library, but how do I do this in a simple sketch?

Since the ide creates the prototypes for you, you have to create an additional cpp source file and header.

I am not sure what happens if you create your own prototype. Try it!

Doesn't this function need 2 things passed in? void test4Fun(int t, int t2=2)

and here you only give it 1: test4Fun(12);

KeithRB: Since the ide creates the prototypes for you, you have to create an additional cpp source file and header.

I am not sure what happens if you create your own prototype. Try it!

The IDE only creates prototypes if you don't. So, do it yourself.

Ok, thanks guys!

For those of you who don't know how to do this : (correct me if there is a better way to do this, but this compiles without errors...

void test4Fun(int t, int t2=1);


void setup()
{

  Serial.begin(9600);
  
  test4Fun(10,11);
  
  test4Fun(12);
}

void loop()
{
  
}


void test4Fun(int t, int t2=2)
{
  Serial.print(t);
  Serial.print(" ");
  Serial.println(t2);
}

I think at the very least your default t2 values should agree between the prototype and the function!

Oops, yes sorry about that.

I got confused. Pasted the wrong version in here. Thanks for that Keith.

Just to make it clear here it is :

void test4Fun(int t, int t2=2);


void setup()
{

  Serial.begin(9600);
  
  test4Fun(10,11);
  
  test4Fun(12);
}

void loop()
{
  
}


void test4Fun(int t, int t2=2)
{
  Serial.print(t);
  Serial.print(" ");
  Serial.println(t2);
}

Hope this helps someone.

Coming from python, I’m finding so many things I wish I could do here with what I do there. This being one of them. Just a heads up that in IDE 1.6.4 you no longer can/need to set the default in the function any longer, it just won’t let you since you already assigned a value in the “prototype”? ← If I understand correctly.

Regardless, thanks for this, I’ve been beating myself up trying to figure out how to set defaults on optional function arguments. I might opt to do a header file. What are the pro’s & con’s for header files? Just wondering what size of a program should I start to consider to put one together.

Thanks Paul for the original help. Thanks Kevin for posting your code. Thanks Drewski for pointing out that you only need to set the default value in the header.

All 3 of you are spot on. My code is working nicely now :)

Drewski:
Coming from python, I’m finding so many things I wish I could do here with what I do there. This being one of them. Just a heads up that in IDE 1.6.4 you no longer can/need to set the default in the function any longer, it just won’t let you since you already assigned a value in the “prototype”? ← If I understand correctly.

Regardless, thanks for this, I’ve been beating myself up trying to figure out how to set defaults on optional function arguments. I might opt to do a header file. What are the pro’s & con’s for header files? Just wondering what size of a program should I start to consider to put one together.

Header files are only needed if you have more than one file. They are so other files can call your functions. Otherwise, as you did, just put the prototypes at the top of the single file you have.