Pages: [1]   Go Down
Author Topic: Functions - return array or multiple variables?  (Read 18555 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 2
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,  I am new to C and am trying to write a function which accepts no input parameters, but returns either a byte array or two separate byte variables, whatever will work.

Here is an example of what I am talking about:
Code:
void loop()
{
  byte data[2] = getdat();
}

byte getdat()
{
  byte dat[2];
  dat[0] = 'a';
  dat[1] = 'b';
  return dat;
}

I have tried some variations of declaring the array and get different errors on compile this version yields this tidbit:

In function 'void loop()':
error: array must be initialized with a brace-enclosed initializer In function 'byte getdat()':  

What am I doing wrong here? Is what I am asking even possible?
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 2
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

After doing some more searching It seems that functions in C are unable to return arrays  smiley-sad. I will try to use  some kind of workaround.
Logged

Forum Administrator
Cambridge, MA
Offline Offline
Faraday Member
*****
Karma: 12
Posts: 3538
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The usual way to handle this in C is to pass the array into the function and modify it.  The modified values will be retained when the function ends.
Logged

Brisbane, Australia
Offline Offline
God Member
*****
Karma: 1
Posts: 593
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

AFAIK you pass a pointer to the array as a parameter to the function and then just edit it normally inside the function.
Logged

USA
Offline Offline
Sr. Member
****
Karma: 0
Posts: 452
Freeduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
AFAIK you pass a pointer to the array as a parameter to the function and then just edit it normally inside the function.
And this is an example of what Cheater is suggesting:

Code:
void loop()
{
  byte data[2];

  getdat(&data);
}

void getdat(byte *pdata)
{
  pdata[0] = 'a';
  pdata[1] = 'b';
}

Warning: getdat() doesn't know the number of elements in the array data, so it is better to pass it as a second argument while calling getdat().

Or you can define the array as a global variable in setup() and use it everywhere.

C is an extremely powerful language... remember that almost 100% of all Unixes and Linuxes are written with it...
Logged

Zürich, Switzerland
Offline Offline
Newbie
*
Karma: 0
Posts: 40
Shall a gadget be useful?
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the code, I slowly start to understand the concept of pointers. My gcc-4.2 accepted the code posted by nkcelectronics but gave a warning (incompatible pointer type) during compiling. The program worked well despite the warning.

The Arduino-environment though didn't compile the code nkcelectronics suggested and aborted with an error (cannot convert pointer type), so I had to change the function-call to «getdat(&data[0])» and then it worked perfectly. It seems, as if you have to specificly hand over the address of the first element of the array.

Code that worked in Arduino:

void loop()
{
  byte data[2];

  getdat(&data[0]);
}

void getdat(byte *pdata)
{
  pdata[0] = 'a';
  pdata[1] = 'b';
}
Logged

Huntsville, Alabama, USA
Offline Offline
Sr. Member
****
Karma: 2
Posts: 327
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That will work.  There's a simpler, and more conventional way to do it:
Code:
void loop()
{
 byte data[2];

 getdat(data);
}

void getdat(byte pdata[])
{
 pdata[0] = 'a';
 pdata[1] = 'b';
}

Either way, the same address gets passed to getdata().  In the function declaration for getdat(), I would use 'byte pdata[]' rather than 'byte *pdata' because I will be referencing pdata as an array rather than a character pointer.  Once again, the way you did it was  equivalent, but saying it's an array makes it a tiny bit more self-documenting.

By the way, if you wanted to change the value of two variables in getdat(), rather than using an array, here's one way to do it:
Code:
void loop()
{
 byte data1;
 byte data2;

 getdat(&data1,&data2);
}

void getdat(byte *d1,byte *d2)
{
 *d1 = 'a';
 *d2 = 'b';
}
Personally, I'd use an array, as you did.

Regards,

-Mike
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 654
Posts: 50931
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You could also declare the function as taking addresses of variables:

Code:
void getdat(byte &one, byte &two)
{
    one = 1;
    two = 2;
}
Then, call it:

Code:
byte a=13, b=26;
getdat(a, b);

After the call, a will contain 1, and b will contain 2.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 15
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
My gcc-4.2 accepted the code posted by nkcelectronics but gave a warning (incompatible pointer type) during compiling. The program worked well despite the warning.
In C an array IS a pointer. You should be calling getdat(data) rather than   getdat(&data);. The latter will probably "work" but will overwrite some random place in memory which will not be where you intended.

("data" is equivalent to "&data[0]", i.e. the address of the start of the array. "&data" is the address of the address of the start of the array)
Logged

Sussex UK / CT USA
Offline Offline
Edison Member
*
Karma: 0
Posts: 1028
Forums forever
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

a) If your example shows ALL you want to do, you could be really, really "bad" and just use some global variables.

b) If you want one function to return two (or more) numbers to one of a limited number of destinations, you could still use global variables ("bad"), but pass a "which destination" switch to the function....



Calls....

Fill2(0);

Fill2(1);

Function's declaration...

void Fill2(byte bWhichDest)
{
if (bWhichDest==0)
  {
     a=whatever;
     b=whatever;
  }
  else
  {
   c=whatever;
   d=whatever;
  };

Not "good programming"... but would get some jobs done...
Logged

Pages: [1]   Go Up
Jump to: