Using functions' name and for

Hello, I wanna use "for" Like the pseudo code below:

Void setup(){
Serial.begin(9600);
}
Void loop(){
For (int i=1; i<=3; i++){
x[i];
delay(2000);
}
}

Void x1(){
......
......
}
Void x2(){
......
......
}
Void x3(){
......
......
}

How can I do it. Thank you

and what is the problem ?

do you want to know how to write for loops ?

If I guess correctly, lotsa ways to do it, but nothing like what you are aiming for exists in C/C++.

You could use a switch/case statement, or just a series of if/else statements.

   if (i == 1) x1();
   else if (i == 2) x2();
...

and so forth.

a7

1 Like

Maybe look up pointers to functions.

1 Like
void x1 (void)  { Serial.println (__func__); }
void x2 (void)  { Serial.println (__func__); }
void x3 (void)  { Serial.println (__func__); }

void (*func[])(void) = { x1, x2, x3 };

void loop () {
    for (int i=0; i<3; i++) {
        func [i] ();
        delay (2000);
    }
}

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

Thank you so much

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.