Is it possible to call a function based on a variable?

Hi all,

I have 3 functions - setupX(), setupY(), setupZ().

I wondered if it was possible to do something along the lines of this.....

void myFunction(char fncLetter){

setup{fncLetter}();

}

And then I could pass in "X", "Y" or "Z" and call the correct function?

Is something like that possible?

A big thank you for any help you can give me with this. I'm completely new to Arduino programming so please excuse my ignorance if this is a bit of a daft question.

I wondered if it was possible to do something along the lines of this.....

No. At run-time, functions do not have names.

You could create an array of function pointers (a google search term for you), and pass an index into the function pointer array, to use to select the function to call.

Another way, easier for beginners ti understand, is to use a switch statement to call the right function

void myFunction(char fncLetter)
{
  switch (fnLetter)
  {
    case 'X': setupX(); break;
    case 'Y': setupY(); break;
    case 'Z': setupZ(); break;
  }
}

But calling them setupX, setupY and setupZ would be quite foolish... Give them a name that makes sense :wink:

septillion:
But calling them setupX, setupY and setupZ would be quite foolish... Give them a name that makes sense :wink:

Hi septillion,

It's a CNC machine that I'm building with 3 axis. X, Y and Z and these are homing routines hence the naming.

marco_c:
Another way, easier for beginners ti understand, is to use a switch statement to call the right function

void myFunction(char fncLetter)

{
 switch (fnLetter)
 {
   case 'X': setupX(); break;
   case 'Y': setupY(); break;
   case 'Z': setupZ(); break;
 }
}

That will be perfect. Thanks Marco that's exactly what I'll use. Thanks for your help :slight_smile:

PaulS:
No. At run-time, functions do not have names.

You could create an array of function pointers (a google search term for you), and pass an index into the function pointer array, to use to select the function to call.

Thanks Paul. I'm going to go with the Switch function but good to know that the functionality is possible with a little lateral thinking. Cheers. :slight_smile:

It's a CNC machine that I'm building with 3 axis. X, Y and Z and these are homing routines hence the naming.

I would NOT expect setupX() to home the x axis. I WOULD expect homeX() to home the x axis.

Agree with paul :slight_smile:

PaulS:
I would NOT expect setupX() to home the x axis. I WOULD expect homeX() to home the x axis.

Yep I agree. There's a bit more to it than that but didn't want to bog the thread down. It was just the principles I was looking for.

Thanks for the advice though. You're correct that it is important to choose easy-to-understand function names.

Thanks

uptown47:
Yep I agree. There's a bit more to it than that but didn't want to bog the thread down. It was just the principles I was looking for.

Thanks for the advice though. You're correct that it is important to choose easy-to-understand function names.

Thanks

It may be worth looking at creating a class and eliminate all that duplicative work.

X.home();
Y.home();
Z.home();

BulldogLowell:
It may be worth looking at creating a class and eliminate all that duplicative work.

X.home();

Y.home();
Z.home();

Hi BDL,

Creating a class would be the perfect solution but, unfortunately, it is way beyond my skill level at the moment. However, once I've got all the functions written I might start looking at what's involved.

Thanks for the idea and advice :slight_smile: