Calling a function with just some of the parameters

From my old memory, in C++ whenever I wanted to use a basic function that had multiple parameters, and wanted to use just some of them I just used something like this.

Void example(int nr=0, char let='z', String message="") 

Void setup() {
} 

Void example(int nr, char let, String message) {
  doing maggic;
} 

Int main() {
 if (sunny outside == true) {
    example(nr=5, message="sun") ;
} 

I create the parameters and allocate them some values, in order to be... Not empty. After that, I would call the function specifying what values I want to send, and other than that, those values would remain as set.
When compiling the code, arduino ide tells me the parameters where not declare in that scope. I am not sure if I'm missing something, or if the compiler does not accept this way of handling what I am trying to accomplish.

This is C and not Python, this is how sendToSerial should be called:

sendToSerial("", "abs");

EDIT: And if you are using an AVR based MCU, you should not use the "String" data type at all.

But how will the function know which parameter I want the value assigned to? For example if I'm having 5 int, 3 char, 2 bool, and I'm just sending 2 int, 1 char and 1 bool?
Or for the other parameters that I'm not interested in I have to mandatory send some values?

It won't unless you wrote the function to tell it that information. The parameters are assigned in sequence.

You will have to specify all parameters from left to the one you want to use:

void doit(arg1 = 0, arg2 = "", arg3 = 0, arg4 = "") { whatever.. };

//Only using arg2
doit(0, "something");

//Only using arg4
doit(0, "", 0, "something");

//Only using arg1
doit(123);

What you tried in the posted image i Python syntax which does not work in C(++).

1 Like

Thank you, that's helpful. Also I'm invading a Rp2040, so I would believe it could handle the String nicely, since I using a lot of text, and using char arrays would be a nightmare for me

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.