Pages: [1]   Go Down
Author Topic: I need help in the Syntax of this  (Read 610 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I need help please anyone
How do we write float array return type function? I have the 3 values as floats in the function right now I can read them through the serial port. But I am not sure how to pass them to the void loop.
Also I would appreciate telling how to call the function there.

Float array[3]  function (arguments)
{
return float array[3]
}
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

My advice is to pass the array by reference, like this:

Code:
void func (float (& args) [3])
{
  args [0] += 1;
  args [1] *= 2;
  args [2] /= 3;
}

void setup ()
  {
  float foo [3] = { 5, 6, 7 };
  func (foo);
  }
void loop () {}
Logged

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

Quote
My advice is to pass the array by reference, like this:
Yep. Functions can not return arrays.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

But an array is just an alternative notation for pointer arithmetic, and arrays are normally passed by reference anyway...

So the following would work the same as Nick's example, with more obvious syntax:

Code:
void func( float args[3] )
{
  args[0] += 1;
  args[1] *= 2;
  args[2] /= 3;
}

void setup()
{
  float foo[3] = { 5, 6, 7 };
  func( foo );
}

void loop()
{
}

This also reinforces the point that you should be careful not to alter the values in an array passed to a function in place, unless you want those changes to affect the array in the calling function as well.
« Last Edit: March 30, 2012, 06:29:48 pm by blackm0k » Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, you are right. I wonder what put that idea into my head? Thanks for the clarification.

In my defense, the earlier suggestion makes it clearer that we are expecting the array to be modified.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am already in a function

I am reading the values serially from matlab using xbees.
The I am calling a function that has  chararray  as argument which separates the characters to 3 float values.
I want to pass back those values to the loop so I can use them.
But I am only allowed return
Logged

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

Code:
void getMeSomeFloats(char *buffer, float &servoVal, float &pwmVal, float &pressureVal)
{
   servoVal = ???;
   pwmVal = ???;
   pressureVal = ???;
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

how to get these values in the void loop.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I dont get it pauls do you mean like this
Code:
void(inData[])
{
servoFloat= x;
pwmFloat= y;
pressure= z;
getMeSomeFloats(inData,servoFloat,pwmFloat,pressureFloat);

}
void loop()
{
   void getMeSomeFloats(char *buffer, float &servoVal, float &pwmVal, float &pressureVal)
  {
   servoVal = ???;
   pwmVal = ???;
   pressureVal = ???;
   }

}[\code]
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

No, I doubt it. Try this please:

http://www.cplusplus.com/doc/tutorial/

http://www.cprogramming.com/tutorial/c++-tutorial.html
« Last Edit: March 31, 2012, 01:41:06 am by Nick Gammon » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

then I guess the only way would be to write 3 functions each time returning one of the values
do you think it is ok this way
Logged

Offline Offline
Jr. Member
**
Karma: 4
Posts: 68
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
here is another solution:

Code:
float *ReturnArray()
{
   static float arr[3]; // must be static!

   arr[0] = 1.1f;
   arr[1] = 2.2f;
   arr[2] = 3.3f;

   return arr;
}

void loop()
{
   float *v = ReturnArray();
   // Use v as an array:
   float sum = v[0] + v[1] + v[2];
}

Logged

Pages: [1]   Go Up
Jump to: