Pages: [1]   Go Down
Author Topic: Sending and Receiving Multiple Variables from a Function  (Read 884 times)
0 Members and 1 Guest are viewing this topic.
Heisenberg won't tell me.
Offline Offline
Newbie
*
Karma: 0
Posts: 26
Arrrrrgghh!!!! Duino.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm slightly foxed. I'm used to writing some pretty basic C, and want to write some functions that do some stuff with some variables, so it helps tidy up my program a lot and I can call the same functions from anywhere. It doesn't seem quite what I'm used to though.

I've worked out I can send stuff to a function by using:

Code:
void functionname(variable1, variable2){
  // Funky Code here
}

If I want to get data back, e.g. an integer, I can define it as:

Code:
int functionname(variable1, variable2){
  // Funky Code here
}

But what if I want to get a few values back from it? Or get a string (or char array), AND an int or float back?

I also gather that I don't need to send memory pointers like I'm used to (&variable1, or use *variable1)?
Logged

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 13
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You do
need to send memory pointers like I'm used to (&variable1, or use *variable1)?
Logged

Global Moderator
Netherlands
Online Online
Shannon Member
*****
Karma: 217
Posts: 13734
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You can also define a struct containing all the variables that you need and let the function return that.

Something like:

Code:
//
//    FILE: struct.pde
//  AUTHOR: Rob Tillaart
//    DATE: 2011-03-07
//
// PUPROSE: show struct as return value
//

struct MS
{
  int size;
  char x[10];
  char c;
}
p;

void setup()
{
  Serial.begin(115200);
  Serial.println("I am Arduino");
}

void loop()
{
  p = f();
  Serial.println(p.size, DEC);
  for (int i=0; i< p.size; i++)
    Serial.println(p.x[i], DEC);
  Serial.println(p.c);
  while(1);
}

struct MS f()
{
  MS q;
  q.size = 5;
  for (int i=0; i< 5; i++) q.x[i] = i*3-1;
  q.c = 'z';
  return q;
}
« Last Edit: March 07, 2011, 01:16:55 pm by robtillaart » Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Heisenberg won't tell me.
Offline Offline
Newbie
*
Karma: 0
Posts: 26
Arrrrrgghh!!!! Duino.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


You can also define a struct containing all the variables that you need and let the function return that.

So I'm correct in thinking you can't receive multiple variables back from the function, so the only way is to make a struct so the "one variable" contains all the variables you need?  smiley-sad-blue  It sounds like a bit of a PITA, but I guess it'd work!

Or if I DO use memory pointers as AlphaBeta suggested is worthwhile (&'s and *'s) and change the variables inside the function (e.g. *Variable1 = x) will it change the values permanently to those I calculate inside the function, even when I return to main? If so I can use a void function, but the variables will be changed anyway?

(Sorry I'm a bit rusty, and I never knew that much to begin with!).
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17294
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well you can always declare some variables global and then they can be read or written to anywhere in the program.

Lefty
Logged

Global Moderator
Netherlands
Online Online
Shannon Member
*****
Karma: 217
Posts: 13734
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The advantage of returning a struct is that a struct is a logical entity. A better example would be a function returning a point = struct { int x; int y; }

Quote
If so I can use a void function, but the variables will be changed anyway?
yes, it is named - call by reference

Quote
Well you can always declare some variables global and then they can be read or written to anywhere in the program.
And this is by far the fastest way!
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Don't use pointers (so very...C), use references!

Edit: I think the IDE doesn't like functions with parameters that are references, so you may need to provide your own prototypes for forward references
« Last Edit: March 07, 2011, 01:54:28 pm by Groove » Logged

Per Arduino ad Astra

Heisenberg won't tell me.
Offline Offline
Newbie
*
Karma: 0
Posts: 26
Arrrrrgghh!!!! Duino.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well you can always declare some variables global and then they can be read or written to anywhere in the program.

Hmmm. I think I might have been right with the "Sorry I'm a bit rusty, and I never knew that much to begin with!" comment.  smiley-red So if I define variables in the main loop they only work there, but if they're defined before the main and setup loops then I can just use them from whatever functions I like? No pointers, or other nonsense required? Just use "void FunctionName()" and the variables get changed globally?  Or do I need to do some special global definition?
Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No, really, please don't use globals - that way lies madness and hair-loss.
« Last Edit: March 07, 2011, 01:58:35 pm by Groove » Logged

Per Arduino ad Astra

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17294
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
So if I define variables in the main loop they only work there, but if they're defined before the main and setup loops then I can just use them from whatever functions I like? No pointers, or other nonsense required? Just use "void FunctionName()" and the variables get changed globally?  Or do I need to do some special global definition?

Yep, works great esp for the small types of applications we use on Arduino projects. You will however draw the graybeards disapproval for ignoring the benefits of 'proper' scoping of variables, second only to the tongue lashing you will get if using the goto command.

I use globals a lot while I'm first starting to write a arduino sketch, and then later may clean it up by moving them if global reach is not really required. But then again no one pays me to program nor is there a boss or peers to please.  smiley-grin

I'm kind of a results oriented guy.

Lefty
Logged

Pages: [1]   Go Up
Jump to: