Go Down

Topic: Sending and Receiving Multiple Variables from a Function (Read 970 times) previous topic - next topic

Piplodocus

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: [Select]
void functionname(variable1, variable2){
  // Funky Code here
}


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

Code: [Select]
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)?

AlphaBeta

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

robtillaart

#2
Mar 07, 2011, 07:03 pm Last Edit: Mar 07, 2011, 07:16 pm by robtillaart Reason: 1
You can also define a struct containing all the variables that you need and let the function return that.

Something like:

Code: [Select]

//
//    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;
}

Rob Tillaart

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

Piplodocus



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!).

retrolefty

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

Lefty

robtillaart

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!
Rob Tillaart

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

Groove

#6
Mar 07, 2011, 07:48 pm Last Edit: Mar 07, 2011, 07:54 pm by Groove Reason: 1
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
Per Arduino ad Astra

Piplodocus


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?

Groove

#8
Mar 07, 2011, 07:55 pm Last Edit: Mar 07, 2011, 07:58 pm by Groove Reason: 1
No, really, please don't use globals - that way lies madness and hair-loss.
Per Arduino ad Astra

retrolefty

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

I'm kind of a results oriented guy.

Lefty

Go Up