Callback keyword and Pass a function as an argument in C++

2024-09-10 , 4 mins read.

We will walk through the concepts of callbacks and passing functions as arguments in C++.

1. What is a "Callback"?

A callback is a function that you pass as an argument to another function, and that function can call (or "invoke") the passed-in function at some point. It's like saying, "I'll pass you this function, and you call it when you need it."

In C++, callbacks are usually implemented using function pointers or, more commonly in modern C++, using std::function and lambdas (anonymous functions).

2. How to Pass a Function as an Argument in C++?

In C++, you can pass functions as arguments using the following techniques:

  • Function Pointers (classic C++ approach)

  • std::function (modern C++ approach, introduced in C++11)

  • Lambdas (anonymous inline functions, introduced in C++11)

Example of Using Function Pointers:

If you have a function myFunction that you want to pass to another function, you can use function pointers.

// Define a function
void myFunction(int x) {
    std::cout << "Called with: " << x << std::endl;
}

// Function that takes a function pointer as an argument
void callWithArgument(void (*callback)(int), int value) {
    // Call the function passed as the callback
    callback(value);
}

int main() {
    // Pass myFunction as a function pointer
    callWithArgument(myFunction, 42);
}

Output:

Example of Using std::function and Lambdas:

In modern C++, std::function is used to pass functions as arguments, and lambdas are inline anonymous functions that can be passed to std::function.

Here’s an example using both:

Output:

3. How to Use a Function as an Argument in C++?

When you want to pass a function as an argument, the function you pass could either be:

  • A regular function

  • A lambda function

  • A function pointer

  • A std::function type, which can hold a variety of callable objects (including lambdas, function pointers, and more)

In KVDB project, we use std::function<void(const KeyValue&)> in the inOrderTraversal function, passing a function (or lambda) that takes a KeyValue and returns void.

Here's an example using std::function in a tree traversal:

Using std::function in Tree Traversal

Output:

Explanation:

  1. Lambda Functions: The inOrderTraversal function accepts a lambda as a callback. This lambda processes each key-value pair during the traversal.

  2. std::function: std::function<void(const KeyValue&)> is used to define the type of the callback function, which takes a KeyValue as an argument and returns void.

Summary:

  • Callback refers to a function passed as an argument to another function, which is called ("invoked") within the receiving function.

  • You can pass functions as arguments using function pointers or std::function (the modern approach).

  • Lambdas are a convenient way to define inline, anonymous functions and pass them as arguments.

Last updated