Pages: [1]   Go Down
Author Topic: Functions in Arrays  (Read 583 times)
0 Members and 1 Guest are viewing this topic.
Ohio, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can I store function calls in an array?

i.e.

Code:
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!
« Last Edit: December 17, 2009, 11:36:38 pm by mcheich » Logged

Global Moderator
Dallas
Online Online
Shannon Member
*****
Karma: 178
Posts: 12288
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Spokane, Washington
Offline Offline
God Member
*****
Karma: 0
Posts: 686
My name is Bob, and I'm an addict.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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]);
}

« Last Edit: December 18, 2009, 12:26:27 am by thoed » Logged

Van Alstyne, TX
Offline Offline
Full Member
***
Karma: 1
Posts: 154
Haven't smoked an Arduino... yet.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.

Logged

0
Offline Offline
Faraday Member
**
Karma: 19
Posts: 3420
20 LEDs are enough
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley
Logged

Check out my experiments http://blog.blinkenlight.net

Ohio, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Ohio, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well, Ok maybe this is a stretch for a my skill level, but I am now trying this

Code:
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?
Logged

SE USA
Offline Offline
Faraday Member
**
Karma: 40
Posts: 3783
@ssh0le
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley

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
Logged


SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 106
Posts: 6378
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
int (*matrixLetters[7][4])() =        
{                                
  {a(), b(), c(), d()},
  {e(), f(), g(), h()},
  :
};
Should just be:
Code:
int (*matrixLetters[7][4])() =        
{                                
  {a, b, c, d},
  {e, f, g, h},
  :
};
Logged

Pages: [1]   Go Up
Jump to: