Arrays and Functions
An argument can also be an array. A function that gets an array can go through it in a loop and, for example, return the sum of its elements:
#include <stdio.h>
int sum(int numbers[], int count) {
int total = 0;
for (int i = 0; i < count; i++) {
total += numbers[i];
}
return total;
}
int main(void) {
int points[] = {8, 5, 10};
int goals[] = {2, 0, 1, 3, 1};
printf("%d\n", sum(points, 3));
printf("%d\n", sum(goals, 5));
return 0;
}This program prints:
23
7The parameter int numbers[] is an array of int values. Its brackets are empty, because the function can get an array of any size. In the call, you write only the name of the array, without brackets: sum(points, 3). With brackets, sum(points[], 3), the program doesn't compile: error: expected expression.
Why the second parameter, count? The function doesn't know how many elements the array has, so you have to tell it. Don't try sizeof(numbers) / sizeof(numbers[0]) inside the function: there, numbers isn't really the whole array (what it is, you will see in the lesson on pointers), so sizeof doesn't count its elements. Clang only warns about it (and this site doesn't show warnings), and the count comes out wrong (here 1, so the function would add up only the first element). Work out the count where the array was created, in main, and pass it to the function. And make sure the count is right: with a bigger count, the function would read past the end of the array, a mistake you know from the lesson on arrays.
Notice where the return is: after the } that ends the loop, when all the elements have been added up. A common mistake is to put it one line higher, inside the loop. It would then end the whole function in the very first pass of the loop. Here, such a program doesn't even compile: non-void function does not return a value in all control paths. With a count of 0, the loop wouldn't run at all, and the function would reach its end without a return.
There is one big difference between arrays and numbers. A number parameter gets a copy, as you saw in the previous exercise. An array is not copied: the function gets the very array that the caller passed. So when a function changes its elements, the changes stay in the array in main:
#include <stdio.h>
void add_to_all(int numbers[], int count, int amount) {
for (int i = 0; i < count; i++) {
numbers[i] += amount;
}
}
int main(void) {
int points[] = {5, 8, 7};
add_to_all(points, 3, 10);
for (int i = 0; i < 3; i++) {
printf("%d\n", points[i]);
}
return 0;
}This program prints:
15
18
17The function added 10 to every element of the array points in main. Why arrays behave like this, you will find out in the lesson on pointers. For now, remember that a function can change the elements of an array you pass to it.
Instructions
- 1
The program doesn't compile, because the function
averagedoesn't exist yet (read the first error). Write it in place of the comment: it has two parameters, an arraynumbersof typeintand itscountof typeint, and returns the average of the elements as adouble: their sum divided bycount. Watch out for whole-number division: keep the sum in a variable of typedouble. The program then prints the average temperatures of two weeks,20.80and12.75. - 2
In the first week, the thermometer showed every temperature 1 degree lower than it really was. Below
average, write a functionadd_to_allwith three parameters: an arraynumbersof typeint, itscountand anamount(bothint). It addsamountto every element of the array and returns nothing. At the end ofmain, beforereturn 0;, calladd_to_all(week1, 5, 1)and then print the average ofweek1again, in the same way as above. The program prints21.80: the function has changed the array inmain.