The Wayback Machine - https://web.archive.org/web/20250108221322/https://www.geeksforgeeks.org/passing-a-function-as-a-parameter-in-cpp/
Open In App

Passing a Function as a Parameter in C++

Last Updated : 16 Dec, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Passing a function as an argument is a useful concept in C++. This concept has already been used while passing a custom comparator function as an argument in sort() function to sort a sequence of objects as per the need. In this article, we will discuss different ways to design functions that accept another function as an argument.

A function can be passed as a parameter with 3 approaches i.e.

  1. Passing as Pointer
  2. Using function<> Wrapper
  3. Using Lambdas

1. Passing Pointer to a Function

A function can also be passed to another function by passing its address to that function; In simple terms, it could be achieved via pointers.

C++
#include <iostream>
using namespace std;

int add(int x, int y) { return x + y; }

int mul(int x, int y) { return x * y; }

int invoke(int x, int y, int (*f)(int, int)) {
    return f(x, y);
}

int main() {
  
    // Pass pointers to add & mul
    // function as required
    cout << "Addition: ";
    cout << invoke(20, 10, &add) << '\n';

    cout << "Multiplication: ";
    cout << invoke(20, 10, &mul);

    return 0;
}

Output
Addition: 30
Multiplication: 200

2. Using function Wrapper

In C++ 11, there is a function template class that allows to pass functions as objects.

C++
#include <bits/stdc++.h>
using namespace std;

int add(int x, int y) { return x + y; }
int mul(int x, int y) { return x * y; }

// Function that takes function as arguemnt
int invoke(int x, int y, function<int(int, int)> f) {
    return f(x, y);
}


int main() {
  
    // Pass required function as parameter
      // using its name
    cout << "Addition: ";
    cout << invoke(20, 10, &add) << '\n';

    cout << "Multiplication: ";
    cout << invoke(20, 10, &mul) << '\n';

    return 0;
}

Output
Addition: 30
Multiplication: 200

3. Using Lambdas

Lambdas in C++ provide a way to define inline, one-time, anonymous function objects. These lambdas can be defined in a place where it is required to pass a function as an argument. 

C++
#include <functional>
#include <iostream>
using namespace std;

// Function that takes a pointer
// to a function
int invoke(int x, int y,
           function<int(int, int)> func) {
    return func(x, y);
}

// Driver Code
int main()
{

    // Define lambdas for addition and
    // multiplication operation where
    // we want to pass another function
    // as a parameter

    // Perform Addition
    cout << "Addition: ";
    int k = invoke(20, 10,
                   [](int x,
                      int y) -> int {
                       return x + y;
                   });

    cout << k << '\n';

    // Perform Multiplication
    cout << "Multiplication:";
    int l = invoke(20, 10,
                   [](int x,
                      int y) -> int {
                       return x * y;
                   });

    cout << l << '\n';

    return 0;
}

Output
Addition: 30
Multiplication:200

Passing Member Function of a Class

Passing a member function of a class is a bit different from the normal functions. If the member is static, we can pass it in the similar way as above function. But if the member functions are a non-static member function, we need to associate an object with member function to pass it to the function.

The below example first binds the member function and then passes it to the function that takes the object of function class as argument. We can also pass the pointer, but it is not recommended.

C++
#include <bits/stdc++.h>
using namespace std;

class C {
public:
    int f(int a, int b) {
        return a * b;
    }
};

void invoke(function<int(int, int)> calc) {
    
      // Call the member function using function
      cout << "Product: " << calc(10, 20);
}

int main() {
    C c;
    
    // Wrapping member function of C
    auto calc = bind(&C::f, &c, placeholders::_1, placeholders::_2);
    
    // Passing it as argument
      invoke(calc);
    
    return 0;
}

Output
Product: 200


Next Article
Practice Tags :

Similar Reads

three90RightbarBannerImg