Functions as Arguments

Can You Pass a Function as a Parameter?

Yes, you can pass a function as a parameter. Learn how function pointer parameters work in C and C++, with a runnable example in each language.

On this page
  1. A function pointer parameter
  2. A template parameter
  3. A std::function parameter
  4. Things to check
  5. The same idea in C
  6. Summary

Yes. A function can take another function as a parameter. In C++, the parameter can be a function pointer, a template type, or a std::function. In C, a function pointer is the standard way to do it. The examples below show the basic pattern in each.

A function pointer parameter

Declare the parameter with the pointer type of the function it should accept. This one accepts a function that takes no parameters and returns nothing:

#include <iostream>

void greet() {
    std::cout << "Hello\n";
}

void run_twice(void (*task)()) {
    task();
    task();
}

int main() {
    run_twice(greet);  // prints Hello twice
}

Inside run_twice, task() calls whatever function was passed in. The caller passes greet without parentheses, which hands over a pointer to greet. Writing run_twice(greet()) would be wrong. It calls greet first and tries to pass its result, which has type void, so the compiler rejects it.

A template parameter

A template parameter accepts any callable that can be called with no arguments, including a lambda:

#include <iostream>

template <typename Task>
void run_twice(Task task) {
    task();
    task();
}

int main() {
    run_twice([]() { std::cout << "Hi\n"; });  // prints Hi twice
}

A std::function parameter

A std::function parameter also accepts a lambda, and that lambda can capture variables:

#include <functional>
#include <iostream>
#include <string>

void run_twice(const std::function<void()>& task) {
    task();
    task();
}

int main() {
    std::string name = "Sam";
    run_twice([name]() { std::cout << "Hi " << name << "\n"; });  // prints Hi Sam twice
}

Things to check

  • The parameter’s type must match the function you pass. A void (*)() parameter won’t accept a function that takes an int.
  • A void (*)() parameter won’t accept a lambda that captures variables. Use a template or std::function for that.
  • Pass the name without parentheses. run_twice(greet) passes the function, and run_twice(greet()) calls it instead.

The same idea in C

C has no templates, lambdas or std::function, so the parameter is a function pointer. Use (void) for a function with no parameters:

#include <stdio.h>

void greet(void) {
    printf("Hello\n");
}

void run_twice(void (*task)(void)) {
    task();
    task();
}

int main(void) {
    run_twice(greet);  /* prints Hello twice */
    return 0;
}

Summary

Yes, a function can be a parameter. Declare the parameter as a function pointer, a template type, or a std::function, and pass the function’s name or a lambda.

The complete guide to functions as arguments

How to Pass a Function as an Argument in C++
Read the guide

Keep exploring

Guides from other topics

All topics →