Arrays and Functions

I want to manipulate values in an array from a function, how do I do that ? Is this the right way of doing it ?

[i]
int numbers[] = {0, 0, 0, 0};
int maxNumbers[] = {10, 10, 30, 55};
byte index = 0;

void setup()
{

}

void loop()
{

     add(1, index); // This is how I would be calling the function

     add(5, 2);  // I also need to be able to increment a specific value in the array regardless the current value of "index"

      // Also I need to test the value of "index" in an switch case -loop

}

void add(int value, byte index)
{
    numbers[index] += value;
    if(numbers[index] > maxNumbers[index]) numbers[index] %= maxValue[index];
}
[/i]

A function can change the value held in a global array just as it can change the value of any global variable. The fun starts when you want to pass an array to a function or return one from it.

That will work, you may also want to check that index (the one in the add function) does not go out of bounds of the numbers’ array

if(index < 0 || index >= sizeof(numbers) / sizeof(int))Serial.println("Out of Bounds!"); //out of bounds error

Ps991:
That will work, you may also want to check that index (the one in the add function) does not go out of bounds of the numbers’ array

if(index < 0 || index >= sizeof(numbers) / sizeof(int))Serial.println("Out of Bounds!"); //out of bounds error

You can add flexibility to the code above by making it a macro. As it stands now, it only works for int data.

#define ELEMENTS(x)  (sizeof(x) / sizeof(x[0]))

// more code...

if (index < 0 || index >= ELEMENTS(numbers) )
   Serial.println("Out of Bounds!");                          //out of bounds error

Now you can use ELEMENTS() with any array type (e.g., int, long, float, etc) and it will correctly determine the number of elements.

That is a neat trick I will keep in mind