system
1
Is there a way to call functions dynamically? For example, if there was a function type...
function arrOfFunctions[] = { firstFunction, secondFunction };
void setup () {
arrOfFunctions1;
}
void firstFunction () {
}
void secondFunction () {
}
Is there any way to do it?
Yes, you can do that. You just need the appropriate typedef for "function".
system
3
Sorry that I'm too much of a noob, but how would I do that? I don't see typedef in the reference. Or is it the same as C?
Yes, Arduino uses C++.
Something like this (compiles, but not tested):
typedef void (*function) () ;
void firstFunction () {
// whatever
}
void secondFunction () {
// whatever
}
function arrOfFunctions[] = { firstFunction, secondFunction };
void setup () {
 arrOfFunctions[0]();
 arrOfFunctions[1]();
}
void loop () { }
1 Like
system
5
Thanks, but shouldn't it be...
function arrOfFunctions[] = { &firstFunction, &secondFunction }
?
system
6
Thanks, but shouldn't it be...
function arrOfFunctions[] = { &firstFunction, &secondFunction }
If you like typing, yes, you can put that, but the name of a function is a pointer to the function, so the address-of operator is redundant.
gladoscc:
Thanks, but shouldn't it be...
function arrOfFunctions[] = { &firstFunction, &secondFunction }
?
No. What I wrote works ... perhaps you should try it?