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
![]()
#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);
}