Go Down

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

mcheich

Dec 18, 2009, 05:35 am Last 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

It's difficult for me to tell what you're trying to accomplish.  Please describe what problem you're trying to solve.

CaptainObvious

#2
Dec 18, 2009, 06:26 am Last 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]);
}


Rusty in Texas

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.


Udo Klein

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

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

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

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

westfw

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