Build the name of a function called

Hello everybody,
(I am a beginner)

I would like to know how to call a function concatenating
a variable to the name of the function,

example: (call the button function by pasting the tag behind the name)

        if(Flag.Key_Detect)   {     // check if key is pressed 
            Flag.Key_Detect = 0;
 
                byte tag = Read_Keypad();   // get button tag number
      
                if (tag==2) {           
                    button2();
                }else if (tag==3) {           
                    button3();
                }else if (tag==4) {           
                    button4();

                    ......

                }else{       
                    button1();                                  
                }
	}

void button1(){ ... }
void button2(){ ... }
void button3(){ ... }
....
void button35(){ ... }

thanks
sorry for my English!

I would like to know how to call a function concatenating
a variable to the name of the function,

You can't do that. Function names are lost at the time of compilation. You can, however, create an array of function pointers and call the functions by using the array index number.

void (*functions[])() = {func1, func2, func3};

void setup()
{
  Serial.begin(115200);
  for (int i = 0; i < 3; i++)
  {
    functions[i]();
  }
}

void loop() {}

void func1()
{
  Serial.println(F("function1"));
}

void func2()
{
  Serial.println(F("function2"));
}

void func3()
{
  Serial.println(F("function3"));
}

It works well :wink:
just a little precision, put the line
" void (*functions[])() = {func1, func2, func3}; "
in the setup, otherwise he does not know it!
many thanks

Interesting.

The code as posted compiles and runs using IDE 1.5.6-r2 which is what I normally use. There is, however, a problem when compiling it under IDE 1.8.2 which is presumably what you are seeing.

It works if you put in explicit function prototypes like this

void func1();
void func2();
void func3();

void (*functions[3])() = {func1, func2, func3};

void setup()
{
  Serial.begin(115200);
  for (int i = 0; i < 3; i++)
  {
    functions[i]();
  }
}

void loop() {}

void func1()
{
  Serial.println(F("function1"));
}

void func2()
{
  Serial.println(F("function2"));
}

void func3()
{
  Serial.println(F("function3"));
}

or if you put the functions at the start of the program like this

void func1()
{
  Serial.println(F("function1"));
}

void func2()
{
  Serial.println(F("function2"));
}

void func3()
{
  Serial.println(F("function3"));
}

void (*functions[3])() = {func1, func2, func3};

void setup()
{
  Serial.begin(115200);
  for (int i = 0; i < 3; i++)
  {
    functions[i]();
  }
}

void loop() {}
void button1(){ ... }
void button2(){ ... }
void button3(){ ... }
....
void button35(){ ... }

are there really 35 possible functions for 35 possible tags?

it seems like passing a variable to a function may be in order...

it seems like passing a variable to a function may be in order...

But we don't know what is required so it is difficult to provide help beyond what was requested.

A function call with a parameter and perhaps a switch/case in the function to run functions with entirely different purposes may well be in order. As is often the case a question has been posed with little or no background as to what the questioner is trying to do.

UKHeliBob:
But we don't know what is required so it is difficult to provide help beyond what was requested.

A function call with a parameter and perhaps a switch/case in the function to run functions with entirely different purposes may well be in order. As is often the case a question has been posed with little or no background as to what the questioner is trying to do.

k++