previous | index | next

Passing Arrays to Functions

Consider the following function, which takes an int array as an argument:

void display(int v[], int n) {
  for (int i = 0; i < n; i++) {
    cout << setw(4) << v[i];
  }
  cout << endl;
}

Suppose we initialize an array and display it like this:

const int SIZE = 10;
int a[SIZE];
    
for (int i = 0; i < SIZE; i++) {
  a[i] = i*i;
}
    
display(a, SIZE);

Output:

   0   1   4   9  16  25  36  49  64  81

previous | index | next