Function with variable number of arguments

Is it possible to create a function with a variable number of passed arguments using this compiler?

I have seen discussions where some C++ compilers handle a variable number of arguments
e.g. Bot Verification

Thanks.

I am experimenting with this code, but the results are not what they should be:

Arg a: 2
Arg b: 4
Arg c: 3840
Arg d: 3841
7687

#include <stdio.h>
//#include <stdargs.h>

int Add(int a,int b,...)
{

//This one handles 4 arguments in total.
int l_ParamVal=0;
int total=0;
int i;

//Declare a va_list macro and initialize it with va_start
va_list l_Arg;
va_start(l_Arg,a);

//The required parameters can also be accessed directly
l_ParamVal = a;
Serial.print("Arg a: ");
Serial.println(l_ParamVal);
if(l_ParamVal != -1)
total = total +l_ParamVal;

l_ParamVal = va_arg(l_Arg,int);
Serial.print("Arg b: ");
Serial.println(l_ParamVal);
if(l_ParamVal != -1)
total = total +l_ParamVal;

l_ParamVal = va_arg(l_Arg,int);
Serial.print("Arg c: ");
Serial.println(l_ParamVal);
if(l_ParamVal != -1)
total = total +l_ParamVal;

l_ParamVal = va_arg(l_Arg,int);
Serial.print("Arg d: ");
Serial.println(l_ParamVal);
if(l_ParamVal != -1)
total = total +l_ParamVal;

va_end(l_Arg);

return total;

}

void setup ()
{
Serial.begin(9600);
Serial.println(Add(2,3,4));

}

void loop() {
  delay(1000);
}

"b" is not part of your variable arguments list, so doesn't need

_ParamVal = va_arg(l_Arg,int);
Serial.print("Arg b: ");
Serial.println(l_ParamVal);
if(l_ParamVal != -1)
total = total +l_ParamVal;

If you declare "int Add (int a,...)"

your sketch will work as is (except for arg d).

I guess that really wasn't a very good example since it didn't really handle a variable number of arguments.

This code is better but the check for the last argument (where the value is -1) doesn't work.

I've been trying to adapt code from:

http://msdn.microsoft.com/en-us/library/kb57fad8(VS.80).aspx and from the Sprite.cpp library.

It works for a while, returns 0 instead -1 from the va_arg macro, returns other garbage values and keeps looping ...

Results:

a: 2
total: 2

a: 3
total: 5

a: 4
total: 9

a: 0
total: 9

a: 0
total: 9

a: 0
total: 9

a: 97
total: 106

a: 32
total: 138

a: 116
total: 254

...etc

#include <stdio.h>
//#include <stdargs.h>

int Add(uint8_t a, ...)
{

//This one handles 4 arguments in total.
int total=0;


//Declare a va_list macro and initialize it with va_start
va_list l_Arg;
va_start(l_Arg, a);

while( a != -1 ) {
  Serial.print("a: ");
  Serial.println(a, DEC);
  total += a;
  Serial.print("total: ");
  Serial.println(total);
  a =  va_arg(l_Arg, int);   // using '...' promotes uint8_t to int
}  
va_end(l_Arg);
return total;

}

void setup ()
{
Serial.begin(9600);
Serial.println(Add(2,3,4));

}

void loop() {
  delay(1000);
}

Returning "-1" when using a signed data type doesn't seem to me to be a sensible strategy. :wink:

I found another example of code that seems more logical - the first argument indicates the number of subsequent arguments. It produces the desired result.

valu: 2 total: 2
valu: 3 total: 5
valu: 4 total: 9
9

:slight_smile:

#include <stdio.h>

int Add(int num, ...) {
  int valu;
  int total=0;

//Declare a va_list macro and initialize it with va_start
va_list argList;
va_start(argList, num);

  for(; num; num--) {
    valu = va_arg(argList, int);
    Serial.print("valu: ");
    Serial.print(valu, DEC);
     total += valu;
    Serial.print("  total: ");
    Serial.println(total);
}  
va_end(argList);
return total;

}

void setup ()
{
Serial.begin(9600);
Serial.println(Add(3,2,3,4));  //first arg is number of subsequent variables

}

void loop() {
  delay(1000);
}