previous | index | next

Functions As Arguments

C++ does not have lambda forms, so we must define them with names:

int square(int x) {
    return x*x;
}

int identity(int x) {
    return x;
}

For a C++ function to accept another function as an argument, its type signature must be specified in its definition:

bool increasingOnIntegerRange(int f(int), int low, int high) { ... }
The function can then be called using, for example:
   increasingOnIntegerRange(square, -3, 3)
or
   increasingOnIntegerRange(identity, 0, 6)

previous | index | next