Offline
Newbie
Karma: 0
Posts: 26
|
 |
« on: March 30, 2012, 04:15:24 pm » |
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
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13897
Lua rocks!
|
 |
« Reply #1 on: March 30, 2012, 04:30:39 pm » |
My advice is to pass the array by reference, like this: 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
Brattain Member
Karma: 314
Posts: 35507
Seattle, WA USA
|
 |
« Reply #2 on: March 30, 2012, 04:46:36 pm » |
My advice is to pass the array by reference, like this: Yep. Functions can not return arrays.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 16
|
 |
« Reply #3 on: March 30, 2012, 06:28:05 pm » |
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: 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
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13897
Lua rocks!
|
 |
« Reply #4 on: March 30, 2012, 06:41:13 pm » |
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
Newbie
Karma: 0
Posts: 26
|
 |
« Reply #5 on: March 30, 2012, 06:47:29 pm » |
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
Brattain Member
Karma: 314
Posts: 35507
Seattle, WA USA
|
 |
« Reply #6 on: March 30, 2012, 06:49:51 pm » |
void getMeSomeFloats(char *buffer, float &servoVal, float &pwmVal, float &pressureVal) { servoVal = ???; pwmVal = ???; pressureVal = ???; }
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 26
|
 |
« Reply #7 on: March 30, 2012, 10:23:41 pm » |
how to get these values in the void loop.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 26
|
 |
« Reply #8 on: March 31, 2012, 01:36:56 am » |
I dont get it pauls do you mean like this 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
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13897
Lua rocks!
|
 |
« Reply #9 on: March 31, 2012, 01:37:57 am » |
|
|
|
|
« Last Edit: March 31, 2012, 01:41:06 am by Nick Gammon »
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 26
|
 |
« Reply #10 on: March 31, 2012, 01:45:19 am » |
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
Jr. Member
Karma: 4
Posts: 68
|
 |
« Reply #11 on: March 31, 2012, 04:33:32 am » |
Hi, here is another solution: 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
|
|
|
|
|
|