How can we make this code work for Arduino?
Thank in advance.
double sum (int num, ... )
{
va_list arguments;
double sum = 0;
va_start (arguments, num);
for (int x = 0; x < num; x++)
{
sum += va_arg (arguments, double);
}
va_end (arguments);
return sum;
}
String append (int num, ... )
{
va_list arguments;
String str = "";
va_start (arguments, num);
for (int x = 0; x < num; x++)
{
str += va_arg (arguments,String);
}
va_end (arguments);
return str;
}
function arrOfFunctions[] = { sum, append };
void setup () {
double testSum = arrOfFunctions[0](1,2,3);
String testSum = arrOfFunctions[1]("a","b","c","d");
}
void loop () { }
westfw
February 19, 2020, 10:18pm
2
Where are you getting the definition of "function" ?
In what way does it not work?
double testSum = arrOfFunctions[0](1,2,3);
Doesn't that need to be more complicated, something like:
double testSum = (*arrOfFunctions[0])(1,2,3);
(Hmm. Perhaps not...)
What makes you think it doesn't work? variadic function support has been a part of the c language since its inception in the '70s, and is absolutely supported on Arduino. I use it all the time....
Regards,
Ray L.
For the sum
function you need to pass the number of values as an int
and then that many double
values. For example:
sum(3, 1.0, 2.0, 3.0);
For the append
function, the same applies except the variadic arguments need to be String
objects:
append(4, String("a"), String("b"), String("c"), String("d"));
(Untested.)
Also, it might be better to make it take plain c-strings (const char *
) so you don't have to pass a copy of each String
object to the function.
You cannot put append() and sum() into an array because they are not the same type.
To use C-style variadic function, you must:
#include <stdarg.h>
Your code with minor modification:
#include <stdarg.h>
double sum (int num, ... )
{
~~ va_list arguments; ~~
~~ double sum = 0;~~
~~ va_start (arguments, num); ~~
~~ for (int x = 0; x < num; x++) ~~
~~ {~~
~~ sum += va_arg (arguments, double);~~
~~ }~~
~~ va_end (arguments); ~~
~~ return sum; ~~
}
String append (int num, ... )
{
~~ va_list arguments; ~~
~~ String str = "";~~
~~ va_start (arguments, num); ~~
~~ for (int x = 0; x < num; x++) ~~
~~ {~~
~~ str += va_arg (arguments,String);~~
~~ }~~
~~ va_end (arguments); ~~
~~ return str; ~~
}
void setup () {
~~ double total = sum(1.0d, 2.0d, 3.0d);~~
~~ String result = append("a", "b", "c", "d");~~
}
void loop () { }
Note: Code compiled but not tested.
Nvm, your code is javascript, C-variadic functions require a bit more works.
gfvalvo
February 19, 2020, 11:56pm
7
jimLee:
Never heard of it.
Sounds complicated.
-jim lee
If you've ever used any of the printf() family of functions, then you've used a Variadic.
gcjr
February 19, 2020, 11:57pm
8
with modifications to append() to used char[] instead of String, the following works for me
printf ("%s: %f\n", __func__, sum (3, 2.2, 3.3, 100.1));
printf ("%s: %s\n", __func__, append (3, "how", " now", " brown"));
as arduino_new already said, beside a compiler error, the following doesn't make sense because the functions have different return types.
function arrOfFunctions[] = { sum, append };
the following compiled, but i was not able to use the function pointers instead of sum() or append() above
double (*pSum) (int, ...) = sum;
char* (*pAppend) (int, ...) = append;
westfw
February 20, 2020, 12:00am
9
You cannot put append() and sum() into an array because they are not the same type.
Would C++ overloading let you do it you specified the first differing argument? (Hmm. Can overloaded functions have different return types?)
String append (int num, String *,... );
double sum (int num, double ... );
westfw:
Would C++ overloading let you do it you specified the first differing argument? (Hmm. Can overloaded functions have different return types?)
String append (int num, String *,... );
double sum (int num, double ... );
No function cannot be overload with only different in return type.
So OP's code was javascript.
Here's the C++ equivalent:
double sum(double value)
{
return value;
}
template<typename... Args> double sum(double value, Args... UserArgs)
{
return value + sum(UserArgs...);
}
String append(String value)
{
return value;
}
template<typename... Args> String append(String value, Args... UserArgs)
{
return value + append(UserArgs...);
}
void setup () {
Serial.begin(9600);
double total = sum(1.0d, 2.0d, 3.0d);
String result = append("a", "b", "c", "d");
Serial.println(total);
Serial.println(result);
}
void loop () { }