Functional Programming in C++

In JavaScript, you can pass a function to a function by defining the function in the parameter list:
functionName(function() { //This is a function declared in another function });
I’ve been looking for a way to do the same thing in C++, but I haven’t yet figured out how to. I’m really trying to avoid having to name each function and give them their own declaration. The following is equivalent to above, but is what I’m trying to avoid:
void functionToBePassedAsParameter() { //This is a function declared in another function } functionName(functionToBePassedAsParameter);

When I read online, I keep seeing people say to use lambda functions, but either that’s not the right way to go about it, or I’m misunderstanding lambda functions. Is someone able to suggest how I should go about this in C++?


If you want to pass a function as a parameter in C++ you’d use what is called a function pointer, or passing a reference to a function.

Here’s the wiki.

Lambda functions work pretty well for this. To use them, #include <functional> at the top of your code. When you have a function that takes in a function, you’d use syntax like this:

#include <functional>
//Callback return type outside, parameter types inside the parentheses.
void iCallYourFunction(std::function<void(int, int, int)> func) {
  func(1, 2, 3);

In code calling this function, a lambda is used (or a regular function of course).

void multiplier(int a, int b, int c) {
  printf("Callback values multiplied were: %d\n", a * b * c);
int main(int argc, char** argv) {
  iCallYourFunction([&](int a, int b, int c) {
    printf("I got values %d, %d, %d passed into this callback.\n", a, b, c);

Note the [&] which allows variables outside the lambda to be used inside of it. & means that values outside the lambda will be referenced. If they are changed by the time the lambda runs, the lambda will use the changed value. See below for an example:

#include <vector>
int main(int argc, char** argv) {
  //List (vector) of functions (std::function)
  std::vector<std::function<void(int, int, int)>> callbacks;
  //Add callbacks for each number from 0 to 9.
  for(int i = 0; i < 10; i++) {
    callbacks.push_back([&](int a, int b, int c) {
      printf("Value of i inside here is %d.\n", i);
  //For each callback, call it with iCallYourFunction
  for(int i = 0; i < callbacks.size(); i++) {

Fun fact: Both C++ and JS both have the same behavior here! You’ll get “10” printed 10 times.
In C++ however, you can make a copy of the value each time the lambda is created. To do this, simply replace [&] with [=] in the example above.
In JS, you can create a new variable each time by changing the loop variable to let i instead of var i.
You can choose some variables to copy and some to reference with [&, =copyThis, =copyThat].
The last kind of capture specifier as they’re called is []. This will disallow using any outside variables, but allow the lambda to act like a regular C-style function pointer, which is great for working with functional C libraries and creating pros::Tasks.
Hopefully this clears up how lambdas work for you!


Thank you for your response. This is exactly what I was looking for. I haven’t yet implemented this because I’m currently debugging something else with my code. Once I solve that issue, I’ll incorporate this and report back.