Optional Function Parameters Passed by Reference: Technique

Is this how to define a function with optional parameters that are passed by reference, so that the function can return mutiple values. I found several threads on the forum - but none dealt with optional parameters passed by reference. If I followed the examples, I would get an error about something like int& and int were incompatible… “void myfunc(int& parm1=0);” would not compile.

error: default argument for ‘int& stack’ has type ‘int’

I got it to work by defining a “dummy” global variable “optArg”, and writing the function prototype like this: “void myfunc(int& parm1=optArg);”.

My question is, is that the correct way to do it? Do you have any recommendations as to how? I know it works (see my code below), but it ‘bothers’ me having to define that dummy variable. Any suggestions?

/*
  SD card acode file read 
  
  Version to test using optional arguments

 */
#include <SPI.h>
#include <SD.h>

int optArg=0; // dummy optional argument

File file_ACode;

// Script object
 int ScriptAction[127];
 int ScriptDescription[127];
 int ScriptOption[127];
 int ScriptStack[127];
 int ScriptSize;
 int MainScript;
 #define EndMain ScriptSize

boolean  readln_ACode(int &action, int &desc=optArg, int &option=optArg, int &stack=optArg) 
{
  String inputstring="";
  int inputvalues[4];
  byte inputchar;
  byte index=0;
  boolean Done=false;
  inputchar='\0';
  while ((! Done) && (file_ACode.available())) {
    inputchar=file_ACode.read();
    if (inputchar==',') {
    // comma found, have chars for number
      inputvalues[index]=inputstring.toInt();
      index ++;
      inputstring="";
    } else {
      if (inputchar == 13 ) {
        // EOL found; last number found
        Done = true;
        inputvalues[index]=inputstring.toInt();
        inputstring="";
      } else { 
        if (inputchar >= '0' && inputchar <='9') {
           // character is a digit; append to string
           inputstring=String(inputstring+String(char(inputchar)));
       } else {  
     }
    }
   }   
  }
  action  =inputvalues[0]; // all Global variables
  desc    =inputvalues[1];
  option  =inputvalues[2];
  stack   =inputvalues[3];
  return file_ACode.available();
}

void setup()
{
  boolean more=true;
  int index=0;
  int filler=0;
  
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  Serial.print(F("Initializing SD card..."));

  pinMode(10, OUTPUT);

  if (!SD.begin(4)) {
    Serial.println(F("initialization failed!"));
    return;
  }
  Serial.println(F("initialization done."));

  if (SD.exists("acode2.csv")) {
    Serial.println("acode2.csv exists.");
  }
  else {
    Serial.println(F("acode2.csv doesn't exist."));
  }

  // open file 
  Serial.println(F("Opening acode2.csv..."));
  file_ACode = SD.open("acode2.csv");
  
  more=readln_ACode(ScriptSize,MainScript);
  Serial.print(F("Script Size....... = "));
  Serial.println(ScriptSize);
  Serial.print(F("Main Script starts @ "));
  Serial.println(MainScript);

if (more) {
Serial.println("More lines available");
}

  while (more) {
    index++;
    more=readln_ACode(ScriptAction[index],ScriptDescription[index],ScriptOption[index],ScriptStack[index]);
    Serial.print(index);
    Serial.print(" ");
  }
Serial.println("");
Serial.println("Done loading...");

}

void loop()
{
  Serial.println("Starting dump");
  for(int index=1;index<=ScriptSize;index++) {
      Serial.println(ScriptAction[index]);
      Serial.println(ScriptDescription[index]);
      Serial.println(ScriptOption[index]);
      Serial.println(ScriptStack[index]);
      Serial.println(F("___"));
  }

  while(true){}
}

As far as I can tell, those parameters by reference are really using pointers, which is the traditional C method. I avoid using them because they lead to various kinds of ambiguity if you are not careful.

I would not combine them with optional arguments. If it was my problem, I’d solve it another way.