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.