How would I include 10 functions into an array?

I want to make an array that will call upon 10 different functions. How would I go about doing that? Something like:

void function1(){
blah
blah
}

void function2(){
blah
blah
}

int array[10] = {function1, function2, ... function10};

I’ve never used an array before and I’m not even sure I’m using an array right. :~
The end result is being able to cycle through the 10 functions one by one with a button press but I needed the functions to be simplified down into something I could increment(++) through.

function pointer like this:

void (*myFunction[2])(void);

void setup()
{
  Serial.begin(9600);
  myFunction[0] = myFunction1;
  myFunction[1] = myFunction2;
}
void myFunction1(void)
{
  Serial.println("Function ONE");
}
void myFunction2(void)
{
  Serial.println("Function TWO");
}
void loop()
{

  if (Serial.available() > 0)
  {
    char myChar = Serial.read();
    if (myChar == 'X')
    {
      for (int i = 0; i < 2; i++)
      {
        myFunction[i]();
      }
    }
  }
}

You can use Purdum’s Right-Left Rule to figure out what complex data definitions are, like BulldogLowell’s:

void (*myFunction[2])(void);

Verbalizing: “myFunction is an array of two pointers to function with void arguments that return void”. See:

http://jdurrett.ba.ttu.edu/3345/handouts/RL-rule.html

BulldogLowell:
function pointer like this:

void (*myFunction[2])(void);

void setup()
{
 Serial.begin(9600);
 myFunction[0] = myFunction1;
 myFunction[1] = myFunction2;
}
void myFunction1(void)
{
 Serial.println(“Function ONE”);
}
void myFunction2(void)
{
 Serial.println(“Function TWO”);
}
void loop()
{

if (Serial.available() > 0)
 {
   char myChar = Serial.read();
   if (myChar == ‘X’)
   {
     for (int i = 0; i < 2; i++)
     {
       myFunctioni;
     }
   }
 }
}

what is the dereference operator for? the “reference” section on here doesn’t explain it very well. What would happen without it?

Google "C++ pointers" and you'll learn way more than anyone could ever explain here.

After that google "C++ pointer to function" for your specific application.

Delta_G:
Google "C++ pointers" and you'll learn way more than anyone could ever explain here.

After that google "C++ pointer to function" for your specific application.

Thanks, ya it's a lot of info! lol Too much for here.

red913:
The end result is being able to cycle through the 10 functions one by one with a button press but I needed the functions to be simplified down into something I could increment(++) through.

What about a simple solution like this pseudo code

// pressing a button increments the variable  funcSelect

switch (funcSelect) {
    case 1:
        function1();
        break;
    case 2:
       function2();
         break;
}

...R

Robin2:

red913:
The end result is being able to cycle through the 10 functions one by one with a button press but I needed the functions to be simplified down into something I could increment(++) through.

What about a simple solution like this pseudo code

// pressing a button increments the variable  funcSelect

switch (funcSelect) {
    case 1:
        function1();
        break;
    case 2:
       function2();
         break;
}




...R

I don't know what you're doing here, it doesn't make sense to me. Sorry.

Offering an alternative approach to a function array, I suppose.

Did you manage to build your function array?

red913:

Robin2:

red913:
The end result is being able to cycle through the 10 functions one by one with a button press but I needed the functions to be simplified down into something I could increment(++) through.

What about a simple solution like this pseudo code

// pressing a button increments the variable  funcSelect

switch (funcSelect) {
    case 1:
        function1();
        break;
    case 2:
       function2();
         break;
}




...R

I don't know what you're doing here, it doesn't make sense to me. Sorry.

Google "C++ switch case"

red913:

Robin2:

red913:
The end result is being able to cycle through the 10 functions one by one with a button press but I needed the functions to be simplified down into something I could increment(++) through.

What about a simple solution like this pseudo code

// pressing a button increments the variable  funcSelect

switch (funcSelect) {
    case 1:
        function1();
        break;
    case 2:
       function2();
         break;
}




...R

I don’t know what you’re doing here, it doesn’t make sense to me. Sorry.

I’m leaving the full quotation for context.

My understanding from what you said is that you want to push a button and, on each successive button push, a different function should be called. My code snippet will do that.

Suppose the variable funcSelect starts at 0 then nothing will happen. If you press the button and it increments the value to 1 then function1() will be called. Next time you press the button funcSelect will be changed to 2 and function2() will be called.

…R