function with optional argument - how?

My question is how do I write a function that has two arguments, one always supplied in a call, and the other argument optional?

I’m using Adafruit’s quad alphanumeric display with backpack (with their library) and apparently, the only way to display text is to put one letter at a time into the display buffer for the corresponding digit. To accomplish this I wrote a function with 2 arguments; the first specifies which text message, the 2nd (which I want to be optional) specifies the index for names stored in an array. Some text messages are just strings so no need for the index argument. I tried declaring the 2nd argument as a ‘default’ but then the compiler doesn’t like the calls that do specify the 2nd argument.

The code for the function is below:

// *** SHOW ALPHA FUNCTION ***
 // 'which' - 0=dayName, 1=monthName, 2="TEMP", 3="MODE"
void alphaDisplay(uint8_t which, byte index =0) {
  char dayName[7][5] = {
    "SUN ","MON ","TUE ","WED ","THUR", "FRI ","SAT "};
  char monthName[12][5] = {
    "JAN ","FEB ","MAR ","APR ","MAY ","JUNE","JULY","AUG ","SEPT","OCT ","NOV ","DEC "};
  String text;
  
  switch(which) {
    case 0:
      text = dayName[index];
      break;
    case 1:
      text = monthName[index];
      break;
    case 2:
      text = "TEMP";
      break;
    case 3:
      text = "MODE";
      break;
  }
  
  // put text into display buffers
  for(uint8_t d=0; d<4; d++) {
    alpha4.writeDigitAscii(d, text.charAt(d));
  }
  alpha4.writeDisplay();  // show it
}

By initializing the argument index I was expecting it to become optional but not so. What can I do to fix this? Thanks in advance for the help.

why do you say

By initializing the argument index I was expecting it to become optional but not so.

You assumption is not wrong - you should not use String class for that though!!

have a look at this (untested - just typed here)

void f(int x = 22, int y = 7)
{
  Serial.print(x);
  Serial.print(" and ");
  Serial.println(y);
}

void setup() {
  Serial.begin(115200);
  f(); // first param defaults to 22 and second parameter will default to 7
  f(3); // second parameter will default to 7
  f(5, 8); // give parameters to replace default values
}

void loop() {}

you can have multiple params with a default value, they need to be at the end of the list and when you call you need to supply all the values (even default ones) until the rest of the list is only defaults

try something like this (untested)

// *** SHOW ALPHA FUNCTION ***
// 'which' - 0=dayName, 1=monthName, 2="TEMP", 3="MODE"

enum : byte {DAYNAME, MONTHNAME, TEMP, MODE};

void alphaDisplay(uint8_t which, byte index = 0) {
  const char *dayName[] = {"SUN ", "MON ", "TUE ", "WED ", "THUR", "FRI ", "SAT "};
  const char * monthName[] = {"JAN ", "FEB ", "MAR ", "APR ", "MAY ", "JUNE", "JULY", "AUG ", "SEPT", "OCT ", "NOV ", "DEC "};
  const char * modePtr = "MODE";
  const char * tempPtr = "TEMP";
  const char * text;

  switch (which) {
    case DAYNAME:
      text = dayName[index]; // would be worth testing if index is an acceptable value
      break;
    case MONTHNAME:
      text = monthName[index];// would be worth testing if index is an acceptable value
      break;
    case TEMP:
      text = tempPtr;
      break;
    case MODE:
      text = modePtr;
      break;
  }

  // put text into display buffers
  for (uint8_t d = 0; d < 4; d++) {
    // alpha4.writeDigitAscii(d, text[d]));
    Serial.print(text[d]);
  }
  // alpha4.writeDisplay();  // show it
}

void setup() {
  Serial.begin(115200);
  alphaDisplay(DAYNAME);
  alphaDisplay(MONTHNAME);
  alphaDisplay(TEMP);
  alphaDisplay(MODE);

  alphaDisplay(DAYNAME, 3);
  alphaDisplay(MONTHNAME, 4);
  alphaDisplay(TEMP, 22); // second param is not used
  alphaDisplay(MODE, 33); // second param is not used
}

void loop() {}