## Arrays in functions

You can use both array indexed variables and entire arrays as arguments to functions.
• Indexed variables: an individual element of an array can be function parameter.
• Entire arrays: All array elements can be passed as 'one entity'.
Return an array
• It can be done, but it is not easy. You will learn all this in chapter 10

### Indexed variables as arguments

• Indexed variable handled same as simple variable of array base type
• Example, if there is a function `half(double x)`, then it can take `array[2]` as an argument: `half(a[2])`. Or it can take `a[i]` as an argument(provide that the computer knows what `i` is), example: `half(a[i])`.
• An indexed variable can be a call-by-value argument or a call-by-reference argument. See the example below.
 `#includeusing namespace std;// return the half of xdouble half(double x);// increase x by incvoid increaseBy(double& x, double inc);int main() { // declare an array double array[] = {3.2, 4.3, 7.2}, inc = 2.9; int size = 3; // size of the array int i; // index used in loops // show the elements of the array cout << "The elements in the array are :\n"; for(i = 0; i < size; i++) { cout << array[i] << " "; } cout << endl<< endl; // function call cout << "Half of a[2] is " << half(array[2]) << ".\n"; // show the elements of the array cout << "The elements in the array after " << "function call half(array[2]) :\n"; for(i = 0; i < size; i++) { cout << array[i] << " "; } cout << endl << endl; // show the elements of the array // after increaseBy function cout << "The elements in the array after " << "function call increaseBy(array[i],inc) :\n"; for(i = 0; i < size; i++) { increaseBy(array[i],inc); cout << array[i] << " "; } cout << endl; return 0;}double half(double x) { return x/2;}void increaseBy(double& x, double inc) { x+=inc;}`
Output
 `The elements in the array are :3.2 4.3 7.2Half of a[2] is 3.6.The elements in the array after function call half(array[2]) :3.2 4.3 7.2The elements in the array after function call increaseBy(array[i],inc) :6.1 7.2 10.1`

### Entire arrays as function arguments

• Formal parameter can be entire array
• Argument then passed in function call is array name
• Called ‘array parameter’
• Send size of array as well
• Typically done as second parameter
• Simple int type formal parameter
 `#includeusing namespace std;// ask the user to input an array with the given sizevoid fillUp(double a[], int size);// display the arrayvoid display(double a[], int size);// find the averagedouble average(double a[], int size);int main() { double score[5]; int size = 5; // ask the user to input score fillUp(score, size); // display the scores cout << "The scores are : \n"; display(score, size); cout << endl; // the average cout << "The average of all the scores is " << average(score,size) << endl; return 0;}void fillUp(double a[], int size) { int i; //index cout << "Enter " << size << " numbers:\n"; for(i = 0; i < size; i++) { cin >> a[i]; } cout << "The last array index used is " << (size - 1) << endl;}void display(double a[], int size) { int i; // index for(i = 0; i < size; i++) { cout << a[i] << " "; }}double average(double a[], int size) { int i; // index double sum = 0; for(i = 0; i < size; i++) { sum+=a[i]; } return sum/size;}`
Output
 `Enter 5 numbers:22.2 35 97.25 87.5 50.3The last array index used is 4The scores are :22.2 35 97.25 87.5 50.3The average of all the scores is 58.45`
Explanation
• The function declaration is `fillUp(double a[], int size)`. It consists of 2 arguments
• first argument : the array `a[]`, with no size specified!
• second argument: the size
• The second argument(the size) is not mandatory. It is recommended, so that the function knows the size of the array. The function can then be used for array with any size.
• To make a function call, example, use ```fillUp(score, size)```. Only the array name is used. No bracket in array arguments.
• An array parameters behaves very much like a call-by-reference parameters. The elements in the array can be changed in a function call!
• The address of the first element is passed to the function.

### const parameter modifier

• values in an array can be changed in a function call.
• Most of the times it is fine but sometimes you want to make sure that your array can't be changed.
Example
 `#includeusing namespace std;// The max score an array is maxScore// display the score in 100% scalevoid showScore(double score[], int size, double maxScore);// copy two functions from previous example// reuse the code, this is something good about writing functions// as the user to input an array with the given sizevoid fillUp(double a[], int size);// display the arrayvoid display(double a[], int size);int main() { double score[5]; int size = 5; // ask the user to input score fillUp(score, size); // display the scores cout << "The scores are : \n"; display(score, size); cout << endl; // display the score again showScore(score, 5, 50); // display the scores cout << "After the function call showScore, " << "the scores are : \n"; display(score, size); cout << endl; return 0;}void showScore(double score[], int size, double maxScore) { int i; cout << "Scores are : \n"; for(i = 0; i < size; i++) { score[i] = score[i]/maxScore*100; // change the array accidentally cout << score[i] <<"% "; } cout << endl;}void fillUp(double a[], int size) { int i; //index cout << "Enter " << size << " numbers:\n"; for(i = 0; i < size; i++) { cin >> a[i]; } cout << "The last array index used is " << (size - 1) << endl;}void display(double a[], int size) { int i; // index for(i = 0; i < size; i++) { cout << a[i] << " "; }}`
Output
 `Enter 5 numbers:22 33 12.3 47.5 40.25The last array index used is 4The scores are :22 33 12.3 47.5 40.25Scores are :44% 66% 24.6% 95% 80.5%After the function call showScore, the scores are :44 66 24.6 95 80.5`
You can change
void showScore(double score[], int size, double maxScore)
```To
```
`void showScore(const double score[], int size, double maxScore)`
After these changes the above program can't be compiled because you accidentally changed the score.
Here is the new program
 `#includeusing namespace std;// The max score an array is maxScore// display the score in 100% scalevoid showScore(const double score[], int size, double maxScore);// copy two functions from previous example// reuse the code, this is something good about writing function// as the user to input an array with the given sizevoid fillUp(double a[], int size);// display the arrayvoid display(double a[], int size);int main() { double score[5]; int size = 5; // ask the user to input score fillUp(score, size); // display the scores cout << "The scores are : \n"; display(score, size); cout << endl; // display the score again showScore(score, 5, 50); // display the scores cout << "After the function call showScore, " << "the scores are : \n"; display(score, size); cout << endl; return 0;}void showScore(const double score[], int size, double maxScore) { int i; cout << "Scores are : \n"; for(i = 0; i < size; i++) { cout << score[i]/maxScore*100 <<"% "; } cout << endl;}void fillUp(double a[], int size) { int i; //index cout << "Enter " << size << " numbers:\n"; for(i = 0; i < size; i++) { cin >> a[i]; } cout << "The last array index used is " << (size - 1) << endl;}void display(double a[], int size) { int i; // index for(i = 0; i < size; i++) { cout << a[i] << " "; }} `

### Functions that returns an array

• Functions cannot return arrays same way simple types are returned
• Requires use of a ‘pointer’
• Will be discussed in chapter 10…
• we can use an extra array argument as the "return"
 `#includeusing namespace std;// reverse array a and store it in another arrayvoid reverse(int a[], int size, int reverse[]);// display the arrayvoid display(int a[], int size);int main() { int size = 5; int a[] = {5,10,15,20,25}; int b[5]; cout << "Before calling the reverse function\n" << "array a is : \n"; display(a,size); cout << endl; reverse(a,size,b); cout << "After calling the reverse function\n" << "array a is : \n"; display(a,size); cout << endl; cout << "array b is : \n"; display(b, size); cout << endl; return 0;}void reverse(int a[], int size, int reverse[]) { for(int i = 0; i < size; i++) { reverse[i] = a[size - 1 - i]; }} void display(int a[], int size) { int i; // index for(i = 0; i < size; i++) { cout << a[i] << " "; }} `
Output
 `Before calling the reverse functionarray a is :5 10 15 20 25After calling the reverse functionarray a is :5 10 15 20 25array b is :25 20 15 10 5`