Catenate String and Int to call a void function

I want to concatenate a string “sp” and an integer ‘q’ to form a call function.

Starting with “loop” I want my sketch to follow this structure:

> void loop() {
>           for (int q=1;q=3;q++){
> 
>                    CALL Function Q; [This would bring together sp, q and ();]
> 
>                     [Several lines of common code];
> 
> }
> 
> }
> 
> void sp1();
> void sp2();
> void sp3();

I realise that I could list sp1(); sp2(); … etc within “loop” but that would mean that each of those functions require the common code block repeated within them which defeats my purpose.

I have attempted to cast “q” as a char or a String but to no avail. Any assistance would be appreciated.

Hello
Rum some tutorials available in the WWW for the items "concatenate a string " and how to " call function"s.
This will definitely help.
Have a nice day and enjoy coding in C++.

typedef void (*function) () ;

void sp0() { Serial.println("sp0"); }
void sp1() { Serial.println("sp1"); }
void sp2() { Serial.println("sp2"); }

function funcs[] = {sp0, sp1, sp2};

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

void loop()
{
  for (int q = 0; q < 3; q++) 
  {
    funcs[q](); 
    delay(1000);
  }
}

Hello
I prefer a slim version coded in C++ :nerd_face:

void sp0() { Serial.println(__func__); }
void sp1() { Serial.println(__func__); }
void sp2() { Serial.println(__func__); }
void (*functions[]) () {sp0, sp1, sp2};
void setup()
{
  Serial.begin(9600);
  delay(500);
}
void loop() {
  for (auto function:functions) function(), delay(1000); 
}

Have a nice day and enjoy coding in C++.

Are sp1,sp2 and sp3 completely different functions? Maybe it would be better to make one function and pass a parameter to it.
You should tell us what you really want to do and post complete code. I suppose there are better solutions.

@MIcroBahner:
Each of spx() is a different function.They hold predefined statements which contain data which would be processed by the mentioned Common code.

Those functions only hold one or more mapXYLED(x, y, R, G, B); statements; there can be no parameter passing.

Thank you for your thoughts but I think that paulpaulson offers the structure which I require.

This is beyond my entry level of C/C++ but I do have some understanding of what this will do.

I shall try that in the morning. Thank you for your time.

What about a simple switch statement to distinguish between these predefined statements?

I think this is a situation we see here often. Beginners have a problem and think they found a solution for this. But they are not able to realize this solution. Instead of presenting the original problem here, they ask how this solution could be realized. But in most cases there is a better solution for the original problem.
I don't thimk you need function pointers for your problem. They are seldom really neccessary.

They're the underpinnings of C++!

1 Like

It would make much more sense to put the common code block into a function, so there is no need to 'repeat' them - just call the function.

to the original question, C++ is a compiled language so you can't generate dynamically a function name by concatenating text and use that to call a function.

Function names are gone once compiled.

What are the original functions like? What's the difference among them, what are the similarities? Do they take the same kind of arguments? Maybe function overloading may be the way to go.

This smells like an x-y problem...

Sounds to me like OP thinks he can create the source code text for a function call, at run-time, and directly use that to call a function. Of course that cannot be done.

The real question is: What is the OP REALLY trying to accomplish?

Thank you all for your different responses.

In the pseudo code that I posted in my opening I did define what I required, a mechanism, akin as possible to extracting consecutive members of an array. Once called a few lines of other code were to be executed.

I saw no need to add superfluous material which would cloud the issue.

I have successfully applied @paulpaulson's ideas, after personal coding adjustments. I now need to revisit @red_car's proposal to overcome similar issues that I had with paulpaulson's (don't have time at the moment); they both initially executed the first function but seemingly not the others even though the serial monitor indicated that they had been visited.

It is only fair that I ensure that red_car's proposal also meets my needs before I declare "solution". I do like that with red_car the for...loop allows stepping; that could be useful.

For those of you interested about where this is going I have this at Project Hub, accepted but not yet listed for "Featured" so I don't know if the link will remain valid.

https://create.arduino.cc/projecthub/Murrayman/excel-for-ws2812-rgb-led-array-animations-8f3b77?ref=user&ref_id=570635&offset=0

In my previous post I indicated that I had not at that point tested @red_car's offering.

Both @red_car and @paulpaulson solutions have solved my problem. It is now to decide which to use; maybe I should archive both for future reference.

Thanks to all for various insights.

1 Like

Hello and good morning
Many thanks for your feedback.
Have a nice day and enjoy coding in C++.