Functions as Arguments

How to Pass a Function as an Argument in C++

Learn three ways to pass a function as an argument in C++: function pointers, templates and std::function, with lambda examples you can compile and run.

On this page
  1. The three options
  2. Passing a function pointer
  3. Passing a lambda
  4. Using a template parameter
  5. Using std::function
  6. Which one should you use?
  7. Common mistakes
  8. Summary

You can pass a function as an argument in C++ in three main ways: as a function pointer, as a template parameter, or as a std::function. Each one suits a different job. This guide shows all three with short examples, then explains how to choose between them.

The examples use C++11 features or later, such as lambdas and std::function. To compile one, run g++ -std=c++17 main.cpp.

The three options

  • Function pointer: accepts a function, or a lambda with no captures, whose signature matches exactly.
  • Template parameter: accepts any callable that the compiler can call with the arguments you pass, including lambdas that capture variables.
  • std::function: wraps any callable with a matching signature and stores it, so you can keep it for later.

Passing a function pointer

A function pointer holds the address of a function. Its type includes the return type and the parameter types, so the signature has to match exactly. The parentheses around the name are required:

int (*op)(int, int);  // op is a pointer to a function that takes two ints and returns an int

Here is a complete example. The apply function accepts either of two functions:

#include <iostream>

int add(int a, int b) {
    return a + b;
}

int multiply(int a, int b) {
    return a * b;
}

// op is a pointer to a function that takes two ints and returns an int
int apply(int (*op)(int, int), int a, int b) {
    return op(a, b);
}

int main() {
    std::cout << apply(add, 3, 4) << "\n";       // 7
    std::cout << apply(multiply, 3, 4) << "\n";  // 12
}

Passing add works because a function name used as an argument turns into a pointer to that function. Writing &add gives the same result.

A using alias can make a long function pointer type easier to read:

using BinaryOp = int (*)(int, int);
int apply(BinaryOp op, int a, int b);

Passing a lambda

A lambda is a function written inline. A lambda with an empty capture list ([]) converts to a function pointer with a matching signature, so the apply above accepts it:

std::cout << apply([](int x, int y) { return x - y; }, 10, 4) << "\n";  // 6

A lambda that captures variables cannot convert to a function pointer, so the function pointer version won’t compile with one. The next two options handle that case.

Using a template parameter

A template parameter accepts any type that can be called with two ints. The compiler generates a separate version of the function for each callable type you use:

template <typename Op>
int apply(Op op, int a, int b) {
    return op(a, b);
}

To try it, replace the earlier apply with this version. The calls shown above still work, and a lambda with captures works too.

This version accepts a plain function, a function pointer, a lambda with captures, or a function object (a class that defines operator()). Because the compiler knows the exact type at compile time, it can often inline the call. The template’s body must be visible where it’s used, so templates usually live in header files. When the callable has the wrong signature, the compiler’s error messages can be long.

Using std::function

std::function is declared in the <functional> header. It holds any callable whose signature matches, including a lambda with captured variables. You write its type as std::function<return type(parameter types)>:

#include <functional>
#include <iostream>

int apply(const std::function<int(int, int)>& op, int a, int b) {
    return op(a, b);
}

int main() {
    int factor = 10;
    auto scale = [factor](int x, int y) { return (x + y) * factor; };
    std::cout << apply(scale, 1, 2) << "\n";  // 30
}

The parameter is a const reference, which avoids an extra copy of the wrapper. std::function is the most flexible of the three, but it hides the callable’s exact type, so calls are usually a little slower than calls through a template.

Which one should you use?

  • Use a function pointer for a simple callback with no captured state, especially when the interface should stay compatible with C code.
  • Use a template when you’re writing generic code, such as a helper that works with many kinds of callables.
  • Use std::function when you need to store the callable, keep different callables in one container, or accept captured lambdas and plain functions through the same parameter.

If you’re unsure, start with the simplest option that fits, and change it only when you need to.

Common mistakes

  • Missing parentheses. int *op(int, int); declares a function that returns an int *, not a pointer to a function. Write int (*op)(int, int); instead.
  • Signature mismatch. A function that takes double parameters doesn’t match int (*)(int, int), and the compiler rejects it.
  • Capturing lambda passed to a function pointer. This won’t compile. Use a template or std::function instead.
  • Dangling captures. A lambda that captures a local variable by reference ([&]) and is called after that variable has gone out of scope has undefined behavior.

Summary

You can pass a function as an argument in C++ with a function pointer, a template parameter, or std::function. Start with the simplest one that fits. Use a template or std::function when you need captured lambdas or stored callbacks.

Passing an array is a separate task with its own rules. See how to pass an array to a function in c for that case.

Keep exploring

Guides from other topics

All topics →

Enums in C++ With Examples

Learn enums in C++ with clear examples: syntax, automatic values, enum class, switch statements, underlying types, and common mistakes to avoid.

Enums 5 min read