Function as a parameter

Lambda expressions

//C has no lambdas: the parameter is a pointer
//to a function that has a name
int twicePlusOne(int x) {
    return x * 2 + 1;
}

void map(int *numbers, size_t count, int (*f)(int)) {
    for (size_t i = 0; i < count; i++) {
        numbers[i] = f(numbers[i]);
    }
}

int numbers1[] = {2, 3, 1};
map(numbers1, 3, twicePlusOne);
//numbers1 is 5, 7, 3

//qsort takes the comparison as a function:
//a positive result puts a after b
int descending(const void *a, const void *b) {
    int x = *(const int *)a;
    int y = *(const int *)b;
    return (x < y) - (x > y);
}

int numbers2[] = {2, 3, 1};
qsort(numbers2, 3, sizeof(int), descending);
//numbers2 is 3, 2, 1

printf("numbers1 is %d, %d, %d\n", numbers1[0], numbers1[1], numbers1[2]);
printf("numbers2 is %d, %d, %d\n", numbers2[0], numbers2[1], numbers2[2]);