question about arrays

hello,

i'm not new in the arduino world but despite everything i'm not an expert in C coding and if is possible i need an help or suggestion for how to handle arrays inside the functions.
I wrote a function that compute 4 values and save them in an array declared as global variable...because in C function can't return arrays.

Now i'd like to try to write a library and i need that this array is not declared as global variable but, inside a function or in other working ways.

In past i studied pointers and i think that maybe i have to use them but, i never really used for something, i forgot them, so now is difficult for me to reason with pointers.

So if someone is kind enough to explain me in which way a function can return multiple variables i will be grateful and happy :slight_smile:

thanks and greetings from italy :wink:

because in C function can't return arrays.

Yes, it can.

So if someone is kind enough to explain me in which way a function can return multiple variables

Reference arguments are passed by address. Pointer arguments are passed by value, but the value is just a pointer, so the function can modify the pointed to space.

int someFunc(int array[], int len, int &one, int &two);

void loop()
{
   int someArray[30];
   int arrLen = 30;

   int outOne, int outTwo;

   int result = someFunc(someArray, someLen, outOne, outTwo);
}

int someFunc(int array[], int len, int &one, int &two)
{
   one = 37;
   two = -14;

   array[0] = 18;
   array[len-1] = 99;
}

Note the need to declare a function prototype, because the Arduino IDE does not correctly define prototypes for functions with reference variables.

thanks i'll go to study reference variables! :wink:

but i don't understand why the function is declared as int type.

int someFunc(int array[], int len, int &one, int &two)

Because I meant for it to return a value, too.

int someFunc(int array[], int len, int &one, int &two)
{
   one = 37;
   two = -14;

   array[0] = 18;
   array[len-1] = 99;

   return 823;
}

it is correct that reference doesn't work with arrays?

But the idea to pass the array as function's argument work well!

it is correct that reference doesn't work with arrays?

Arrays are passed by reference by default. There is no reason to explicitly tell the compiler to pass the array by reference, and, in fact doing so causes compiler errors.