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
7

The 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 next 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
17

The function added 10 to every element of the array points in main. Why arrays behave like this, you will find out in the next lesson, on pointers. For now, remember that a function can change the elements of an array you pass to it.

Instructions

The program doesn't compile, because the function average doesn't exist yet (read the first error). Write it in place of the comment: it has two parameters, an array numbers of type int and its count of type int, and returns the average of the elements as a double: their sum divided by count. Watch out for whole-number division: keep the sum in a variable of type double. The program then prints the average temperatures of two weeks, 20.80 and 12.75.

In the first week, the thermometer showed every temperature 1 degree lower than it really was. Below average, write a function add_to_all with three parameters: an array numbers of type int, its count and an amount (both int). It adds amount to every element of the array and returns nothing. At the end of main, before return 0;, call add_to_all(week1, 5, 1) and then print the average of week1 again, in the same way as above. The program prints 21.80: the function has changed the array in main.

Start programming for free

By signing up, you agree to the Terms of Service and Privacy Policy.

Or sign up with:

9/12