Passing optional variables to functions

I need to pass a different number of arguments to a function in my sketch. Can I declare optional variables, or overload the function?
If it matters, I am using an UNO R3.

Welcome to the forum

Yes, you can do either of these

Under some circumstances it may be necessary to provide an explicit function prototype where optional parameters are being used

Greetings!

I currently have this function declared:
void CarDataInput(byte btMsgsIndx, byte *btDataBuffer)

I call it as follows:
CarDataInput(cnstInpMsgsIndx, btDataBuffer);

Periodically, but not frequently, I wish to return the value of a variable of type byte that is declared inside the CarDataInput() funtion.

I could declare this variable in the global scope, but I do not like to do that. I would just like to pass a variable declared in the same scope as the function call. This need is for less than 10% of the time.

Your thoughts, please?

That is a different question than your original one

What determines whether the value will be returned or not ? Do you know to expect a return value or not when you call the function

You could, of course, return a dummy value to indicate that the returned value is not significant. A negative value would fit the bill but would require a signed return type such as int rather than byte

like this..

void CarDataInput(byte btMsgsIndx, byte *btDataBuffer, byte *someTimes = nullptr){
  *btDataBuffer = 10;
  if (someTimes != nullptr){
    *someTimes=100;
    *btDataBuffer = 20;
  }
}

have fun.. ~q

That's great, but it turns out that is not what @ckvasnicka wants to do after all

here's the full test sketch..



void CarDataInput(byte btMsgsIndx, byte *btDataBuffer, byte *someTimes = nullptr){
  *btDataBuffer = 10;
  if (someTimes != nullptr){
    *someTimes=100;
    *btDataBuffer = 20;
  }

}

byte indx,buff;


void setup() {
  // put your setup code here, to run once:

Serial.begin(115200);
byte st=0;
CarDataInput(indx,&buff);
Serial.print("2 Params ");
Serial.println(st);
CarDataInput(indx,&buff,&st);
Serial.print("2 Params ");
Serial.println(st);



}

void loop() {
  // put your main code here, to run repeatedly:

}

Uno Somtimes..

seems to do what @ckvasnicka described, but yes, specs tend to change on the fly around here.. :slight_smile:

have fun.. ~q

Did you see this ?

Yes, someTimes would only get changed if sent it..
and I also understand where you are coming from..
I myself see it as a procedure not a function hence it will never return anything..
Still I think what I presented will work for them..
it's one way anyways..
~q

My original idea was that if i wanted to access the above-mentioned value, I would
merely send a third variable in the function call to grab it, and if I wasn't interested, I wouldn't.

Hence, my question concerning optional variables, vs. overloading the function. Does this sound plausible?

@qubits-us, in your example, can someTimes be omitted when not needed?

Thank you both for your input!

I don't understand why overloading the function wouldn't be a straightforward and appropriate solution. The way you describe the need, doesn't seem much different from the general need that overloading was designed to address.

why not return it? the return value can be ignored

2 Likes

Yes, as I did in the example..

good luck.. ~q

Thank you all for the discussion. It's always good to talk with someone when trying to devise a solution. I think I have a plan, now.

Regarding my original questions, I searched all through the arduino reference, and could not find a single word on optional variables or overloading. There are so many C-programming topics not addressed.

That is because it is an Arduino reference, not a C/C++ reference of which there are plenty on line

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