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.
septillion:
But calling them setupX, setupY and setupZ would be quite foolish... Give them a name that makes sense
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
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.
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.