Call dynamically created function name

Hi

I'm trying this:

String Func;
int Delay = 1000;

void setup() {
}

void loop() {
	Func = "Func";
	Func += random(1,4);
}

void Func1()
{
	delay(Delay);
}
void Func2()
{
	delay(Delay);
}
void Func3()
{
	delay(Delay);
}

The value of the string "Func" is successfuly having a random number appended, but the function itself is not called (the delay is not executed). I cannot just execute

  Func;

Can anybody help me in creating dynamically function name calls with the random number? I trying to avoid to hardcode an array with the function names like here http://arduino.cc/forum/index.php/topic,40842.0.html

Thanks in advance

int Delay = 1000;

void Func1()
{
	delay(Delay);
}
void Func2()
{
	delay(Delay);
}
void Func3()
{
	delay(Delay);
}

void (* funcs[])() = {Func1, Func2, Func3};

void setup() {

}

void loop() {
    digitalWrite(13, HIGH);
    funcs[random(4)]();
    digitalWrite(13, LOW);
    funcs[random(4)]();
}

Any text that isn't between quotes ("") are completely gone after the compiler runs. You need to use an array of function pointers, or something like a switch statement

Thanks for your answer. Since I don't have my hardware yet I tested your code in the Simulator and it does not work. It says "funcsrandom(4);" is an unknown command.

You don't have enough elements in "funcs" - "rand(4)" returns 0..3 inclusive.
Otherwise, the code is fine.

AWOL:
You don't have enough elements in "funcs" - "rand(4)" returns 0..3 inclusive.
Otherwise, the code is fine.

Doesn't look like, I got the same error even if I set the exact amount of elements -> random(3) (which is 0-2)

But back to main topic: So there is no way to call a function name which as been created dynamically by a string?

Doesn't look like, I got the same error even if I set the exact amount of elements

Then I would suggest the error is the fault of the simulator.

The following code appears to work exactly as expected

void Func0()
{
  Serial.print ('G');
}
void Func1()
{
  Serial.print ('A');
}
void Func2()
{
  Serial.print ('T');
}
void Func3()
{
  Serial.print ('C');
}

void (* funcs[])() = {Func0, Func1, Func2, Func3};

void setup() 
{
  Serial.begin (9600);
}

void loop() 
{
    funcs[random(4)]();
    delay(1000);
}

But back to main topic: So there is no way to call a function name which as been created dynamically by a string?

I can't think of a compiled language it would be possible with.

Tell us what you want to do, not how you think it should be done.

On your hardware? If yes, the http://www.arduino.com.au/Simulator-for-Arduino.php is crap

AWOL:
Tell us what you want to do, not how you think it should be done.

I want call functions randomly.

On your hardware?

Running here on a 168-based Duemilanove.

If yes, the http://www.arduino.com.au/Simulator-for-Arduino.php is crap

I've never used it or any other simulator, so I cannot comment.

I want call functions randomly.

That's exactly what has been demonstrated.

Call dynamically created function name

I have a better idea: rather than just dynamically create function names, dynamically create a compiler that compiles your dynamically created code that is dynamically flashed into a dynamically powered arduino so that it dynamically responds to inputs that are dynamically applied to dynamically assigned pins with dynamically changing frequencies and dyanmically altered amplitude.

All dynamically done, all the way.

dhenry:

Call dynamically created function name

I have a better idea: rather than just dynamically create function names, dynamically create a compiler that compiles your dynamically created code that is dynamically flashed into a dynamically powered arduino so that it dynamically responds to inputs that are dynamically applied to dynamically assigned pins with dynamically changing frequencies and dyanmically altered amplitude.

All dynamically done, all the way.

Ok, I got it XD :smiley:

Anyway thanks for all the help

rediculum:
Can anybody help me in creating dynamically function name calls with the random number? I trying to avoid to hardcode an array with the function names like here

I don't understand why you want to avoid using an array of function pointers. It is a perfectly reasonable way to achieve what you're asking for.

An alternative, which takes more code but avoids using those scary function pointers, is to use a switch:

// uncompiled, untested
switch(random(4))
{
    case 0:
        func0();
        break;
    case 1:
        func1();
        break;
    case 2:
        func2();
        break;
    case 3:
        func3();
        break;
}

But back to main topic: So there is no way to call a function name which as been created dynamically by a string?

on contrary you can work around it and (sort of) call by name but it cost you extra overhead.

void callFuncByName(char *name, int param)
{
  if (strcmp(name, "func0")==0) func0(param);
  else if (strcmp(name, "func1")==0) func1(param);
  else if (strcmp(name, "func2")==0) func2(param);
  else if (strcmp(name, "func4")==0) func4(param);
}

Of course you can iterate over an array of function names or over an array of structs that contain the name and the functionpointer etc.

And yes comparing the reverse name would fail much faster :wink: