Weird behaviour in a function with optional paramaters with a default value.

Hi guys,
I've been working for some time on a pretty complex programming for a custom PCB I've designed. The PCB is a controller for escape room props.

Since the code is pretty long (sitting at around 2000 lines right now), I've created a function to be able to quickly add Serial.print points in order for me to be able to debug the code easily. This function only works if the variable debug equals true, so I can change a single variable and get rid of all the serial.prints so I can compile a production ready version of the code.

The function is as follows:

void debug_print(char * text, int value=2147483647)
{
  if(debug)
  {
    Serial.print("Debug: ");
    Serial.print(text);
    if(value != 2147483647)
    {
      Serial.println(value);
    }else{
      Serial.println();
    }
  }
}

For some reason whenever I don't specify a value when calling this function the printes line on the Serial monitor ends with a -1.

For example, if I were to call:

debug_print("this is a test");

The result in the Serial Monitor would be:

Debug: this is a test-1

This has been bothering me for a couple of weeks, but now I am working on a function that displays a message in an oled screen and a similar thing happens. When I modified the function to add an optional parameter, I started getting the -1 in the oled screen.

Can anyone tell me where this "-1" is coming from? How can I get rid of it? What am I doing wrong?

Thansk in advance for all your help! It is really appreciated!

Did you mean,

void debug_print(char * text, long value=2147483647)

?
That number overflows an int type.

The value range for a signed int is -32768 to 32767.

2147483647 == 0x7FFFFFFF
When you try to stuff that into a 16-bit int it gets truncated to 0xFFFF.
0xFFFF == -1

aarg:
Did you mean,

void debug_print(char * text, long value=2147483647)

?
That number overflows an int type.

Yes, in fact that’s what I meant. Of course it had to be some stupid mistake right in front of my eyes!
Thank you very much guys!