Void arguments optional

Hello.
I'm a beginner in arduino, and I have a doubt in relation to the "void" function.
Is it possible to have several arguments in "void" and hide the last ones in case it is not necessary to use?
Example:

void dig(int pos, uint32_t color1, uint32_t color2, uint32_t color3 ) {
... }

Here I would like to use a color ,uint32_t color1 :
dig(14, red);
and that the others (uint32_t color2 and uint32_t color3) already had a predefined value.
but so in the error compilation because the other 2 arguments are missing (uint32_t color2 and uint32_t color3).

How can I make the arguments (uint32_t color2 and uint32_t color3) optional ???

Assign default values to the last 2 parameters...

void dig(int pos, uint32_t color1, uint32_t color2 = 0, uint32_t color3 = 0) {
... }
1 Like

Also look up function overloading.

1 Like

Good suggestions have been made.
Presented solutions will work in any function, not only void functions...
A void function is a function that does not return anything (it returns emptyness). An int function returns an int etc.
In good old pascal a void function was named a procedure.... a set of instructions to do things... whereas a function is a set of instructions where some result is sent to the caller... (sometimes only a message to acknowledge that the instructions were performed successfully)

1 Like

Thanks for the answer.
I tried it this way with this simple code:

void setup() {
  Serial.begin(115200);
}
void loop() {
  dig(5, 8);
  delay(1000);
}
void dig(uint16_t color1, uint16_t color2 = 0  , uint16_t color3 = 0) {
  if (color1 > 2) {
    Serial.print ("red = "); Serial.println (color1 );
  }
  if (color2 > 2) {
    Serial.print ("green = "); Serial.println (color2);
  }
  if (color3 > 2) {
    Serial.print ("blue = "); Serial.println (color3);
  }
}

And in the compilation it returns this error:

"error: 'dig' was not declared in this scope
dig(5, 8);
^
exit status 1
'dig' was not declared in this scope"

But if I change the position of the "void dig" to before the "void loop" it works perfectly:

void setup() {
  Serial.begin(115200);
}
void dig(uint16_t color1, uint16_t color2 = 0  , uint16_t color3 = 0) {

  if (color1 > 2) {
    Serial.print ("red = "); Serial.println (color1 );
  }
  if (color2 > 2) {
    Serial.print ("green = "); Serial.println (color2);
  }
  if (color3 > 2) {
    Serial.print ("blue = "); Serial.println (color3);
  }
}
void loop() {
  dig(5, 8);
  delay(1000);
}

Is there any way to be able to use this "void dig" after the "void lloop" ???

Thanks for the answer.
I've already learned something else.

Thanks for the answer.
I use "void" to avoid repeated long code. So I can reduce the size of the code and it is better organized.

Yes, but you'll have to declare a function prototype.

As part of its "beginner friendliness", the Arduino IDE will most times automatically generate function prototypes for any functions that don't already have one in your code as part of the sketch preprocessing step.

However, defining the default parameter value in the function definition causes the Arduino IDE to skip automatic prototype generation for that function. In order to define default arguments you'll need to write your own function prototype or define the function ahead of where it is called as you have done.

1 Like

If placing the function after the loop(), remove the parameter's default values. Then, add a function prototype to before setup() that includes the defaults.

unsigned int color1, color2, color3;

void dig(uint16_t color1=0, uint16_t color2 = 0, uint16_t color3 = 0); 

void setup() {
  Serial.begin(115200);
}

void loop() {
  dig(5, 8);
  delay(1000);
}

void dig(uint16_t color1, uint16_t color2, uint16_t color3) {

  if (color1 > 2) {
    Serial.print ("red = "); Serial.println (color1 );
  }
  if (color2 > 2) {
    Serial.print ("green = "); Serial.println (color2);
  }
  if (color3 > 2) {
    Serial.print ("blue = "); Serial.println (color3);
  }
}
1 Like

Thank you very much for the reply.
I'm always learning and that's what I love.
And with the example of the dlloyd everything became clearer.

Thank you very much for the reply.
With your example I was able to solve the issue.

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