Go Down

Topic: I need help in the Syntax of this (Read 680 times) previous topic - next topic

fayadAli

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

Nick Gammon

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

Code: [Select]
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 () {}
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

PaulS

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

Yep. Functions can not return arrays.

blackm0k

#3
Mar 31, 2012, 01:28 am Last Edit: Mar 31, 2012, 01:29 am by blackm0k Reason: 1
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: [Select]

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.

Nick Gammon

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.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

fayadAli

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

PaulS

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

fayadAli

how to get these values in the void loop.

fayadAli

I dont get it pauls do you mean like this
Code: [Select]

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]

Nick Gammon

#9
Mar 31, 2012, 08:37 am Last Edit: Mar 31, 2012, 08:41 am by Nick Gammon Reason: 1
No, I doubt it. Try this please:

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

http://www.cprogramming.com/tutorial/c++-tutorial.html
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

fayadAli

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

ea123

Hi,
here is another solution:

Code: [Select]

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


Go Up