Go Down

### Topic: Functions in Arrays (Read 2007 times)previous topic - next topic

#### mcheich

##### Dec 18, 2009, 05:35 amLast Edit: Dec 18, 2009, 05:36 am by mcheich Reason: 1
Can I store function calls in an array?

i.e.

Code: [Select]
`int matrixLetters[7][4] =                // a matrix that holds the functions{                                  {a(), b(), c(), d()},  {e(), f(), g(), h()},  {i(), j(), k(), l()},  {m(), n(), o(), p()},  {q(), r(), s(), t()},  {u(), v(), w(), x()},  {y(), z(), y(), z()}};`

Thanks!

#### Coding Badly

#1
##### Dec 18, 2009, 06:00 am
It's difficult for me to tell what you're trying to accomplish.  Please describe what problem you're trying to solve.

#### thoed

#2
##### Dec 18, 2009, 06:26 amLast Edit: Dec 18, 2009, 06:26 am by thoed Reason: 1
Quote
It's difficult for me to tell what you're trying to accomplish.  Please describe what problem you're trying to solve.

lol:
Quote
Can I store function calls in an array?

I'm not sure you'll be able to store Functions into arrays.. can't say I've tried.. but have you thought about just using ASCII values 'a' to 'z' and then just use a Case statement for the function?

For example:
Quote

char myFuncs[] = {
'a','b','c'};  // etc etc.. up until z

void myFunction(char Command)
{
switch(Command)
{
case 'a':
// do something here
break;
case 'b':
// do something else here!~
break;
case 'c':
// do another something here
break;
}
}

void setup()
{
}

void loop()
{
myFunction(myFuncs[0]);
}

#### Fe2o3Fish

#3
##### Dec 18, 2009, 06:47 am
Yes, but each of the functions in the array have to take the same number
and type of arguments as well as have the same data type returned.
Do a google on 'C function pointers' but here's an example:
Code: [Select]
`int func1(void), func2(void), func3(void);int (*array_name[])() = { func1, func2, func3 };int x;x = (array_name[1])();`
That last line would be how you'd call one of the functions in the array.
Mind you, it's only a single dimension array but you get the idea.
And naturally, you will have to define func1(), func2, and func3().

Is this enough to give you the idea?  It's probably difficult for a beginner.

#### udoklein

#4
##### Dec 18, 2009, 07:18 am
You may want to read this tutorial http://www.newty.de/fpt/index.html. And there are some pitfalls hidden that are not mentioned in the tutorial. For example you have to ensure that the functions actually do exist. This sounds trivial but if the only calls are indirect calls via pointers the linker might decide to not link the functions. The simplest solution is to have some dummy call somewhere such that the compiler and the linker believe that the function is actually called. The proper solution is of course to fix the linker settings accordingly.

Under any circumstances: this is a powerful but not at all a beginner level technique. That is: do not give up to early
Check out my experiments http://blog.blinkenlight.net

#### mcheich

#5
##### Dec 18, 2009, 04:35 pm
Thanks for all the input, currently digesting the tutorial that Undo Klein posted - what I am trying to do is select one function out of a pool of possible functions, in a semi-random matter.  I am sure there are many ways to do this, but right now, I am thinking learning about the function pointers will be both useful and interesting.

#### mcheich

#6
##### Dec 23, 2009, 05:04 am
Well, Ok maybe this is a stretch for a my skill level, but I am now trying this

Code: [Select]
`int (*matrixLetters[7][4])() =         {                                  {a(), b(), c(), d()},  {e(), f(), g(), h()},  {i(), j(), k(), l()},  {m(), n(), o(), p()},  {q(), r(), s(), t()},  {u(), v(), w(), x()},  {y(), z(), y(), z()}};`

I have all of the functions in the array pointer already defined else where.
I am getting the following error:

"error: void value not ignored as it ought to be"

Any thoughts?

#### Osgeld

#7
##### Dec 23, 2009, 05:17 am
Quote
what I am trying to do is select one function out of a pool of possible functions, in a semi-random matter.

function pointers do sound interesting and quite useful, I am glad to be aware of them

but to do what you described in the quote above, make and use a function that passes a semi-random integer, then use switch/case
http://arduino.cc/forum/index.php?action=unread;boards=2,3,4,5,67,6,7,8,9,10,11,66,12,13,15,14,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,86,87,89,1;ALL

#### westfw

#8
##### Dec 23, 2009, 06:22 am
Code: [Select]
`int (*matrixLetters[7][4])() =        {                                  {a(), b(), c(), d()},  {e(), f(), g(), h()},  :}; `
Should just be:
Code: [Select]
`int (*matrixLetters[7][4])() =        {                                  {a, b, c, d},  {e, f, g, h},  :}; `

Go Up