Passing array as parameter

I want to pass an array as a parameter of a function, then have that function change the values in the array, and have those values be available to use from the original function. I'm not sure if this is done with pointers, I wasn't sure. Here's an example of what I'm trying to do.

void(){
  int MyArray[10];
  FillArray(MyArray);

  for(int i=0; i<10; i++){
    Serial.println(MyArray[i]);
  }
}

void FillArray(int TheArray[]){
  for(int i=0; i<10; i++){
    TheArray[i] = 10 * i);
  }
}

Will this work. Is there anything tricky I need to be worried about. Would the same scenario work with a 2 dimensional array?

You need to understand how values are passed to functions. Some values are passed by value. Some are passed by reference.

Passing by value means that the value is copied and the copy passed to the function. Pass by reference means that the address of the variable is passed to the function, so that the function can modify the original.

You can cause pass-by-value to become pass by reference by using the & symbol.

void funWithRefArg(int &val)
{
}

This function will be able to directly modify that value in the variable from the caller, not just the local value in the function.

Arrays are always passed by reference, so the & is not needed. As written, your function will modify the passed in array. Keep in mind, though, that MyArray is a local variable (in your example) that will go out of scope when loop ends, and will be re-created when loop is called again.

When you use a name of the array as an argument in a function call, the compiler treats it as a pointer whose value is the address of the first element of the array using pointer notation or using array notation.

The function can use the value of that pointer to access (read and/or write) any element in the array.

Since the function has no way of knowing the size of the array unless you tell it, the length of the array is commonly used as a parameter (in addition to the name of the array).

void setup()
{
    Serial.begin(9600);
}


void loop()
{
    int MyArray[10];

    FillArray(MyArray, 10);

    for(int i = 0; i< 10; i++){
        Serial.println(MyArray[i]);
    }

    delay(1000);  // Or whatever else you want to do in loop()
}

void FillArray(int TheArray[], int n)
{
    for(int i = 0; i < n; i++){
        TheArray[i] = 10 * i;
    }
}

Output:


0
10
20
30
40
50
60
70
80
90

Regards,

Dave

Footnote:
In C++ you can not have a reference to an array. The language does not allow it.

In C and C++ you are not "passing an array" to a function by using the name of the array as an argument. When you use the name of the array, it is taken to be a pointer whose value is the address of the first element of the array. You pass the value of that pointer to the function.

When people say that you are "passing an array by reference," this is generic terminology used in computer science, but it is not correct C++ terminology.

In C++ there are things called "reference variables" and there is such a thing as a "pass by reference" parameter for functions. These terms have specific definitions that do not describe the "array-passing" mechanism of the language.

2 Likes