Open In App

Passing a Function as a Parameter in C++

Last Updated : 25 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Passing a function as an argument is useful in dynamically changing the behaviour of the function. 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.

Function that is passed as argument is called callback. In C++, there are two ways to pass a function as a parameter:

  1. Passing as Pointer
  2. Using Function Wrapper

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.

Syntax:

C++
return_type (*pointer_name)(prm1, prm2)

where

  • return_type: data type of the value that the function returns.
  • pointer_name: Name of the function pointer.
  • prm1 & prm2: Parameters that function accepts.

Example:

C++
//Driver Code Starts{
#include <iostream>
using namespace std;

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

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

//Driver Code Ends }

// Function invoke takes a 
// pointer to the function
int invoke(int x, int y, int 
            (*f)(int, int)) {
    return f(x, y);
}

int main() {
  
    // Pass address of add & mul
    // function 
    cout << invoke(20, 10, &add) << '
';
    cout << invoke(20, 10, &mul);

//Driver Code Starts{

    return 0;
}
//Driver Code Ends }

Output
30
200

Using Function Wrapper

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

Syntax:

C++
function< rtype_type (prm1, prm2)> function_name

Example:

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 address of add & mul
    // function 
    cout << invoke(20, 10, &add) << '\n';
    cout << invoke(20, 10, &mul);
    return 0;
}

Output
Addition: 30
Multiplication: 200

Lambdas with Function Wrapper

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. 

Example:

C++
//Driver Code Starts{
#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);
}

int main() {

//Driver Code Ends }

    // Define lambdas for addition 
    // operation and another
    // function as a parameter
    int k = invoke(20, 10,
                   [](int x,
                      int y) -> int {
                      return x + y;
                   });
    cout << k;

//Driver Code Starts{
    return 0;
}
//Driver Code Ends }

Output
30

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 << 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
200

std::function vs Function Pointers

Differences between std::function and function pointers are shown below:

Function Pointerstd::function
Not type-safe. Type must match exactly between declaration and usage.Type-safe; ensures correct signature of callable objects.
Less flexible. Only supports function pointers, not objects or lambdas directly.More flexible. Supports function pointers, lambdas, and functors.
Requires explicit declaration of function signature.Easier to use; std::function can be used to wrap any callable type.
No built-in memory management for the function.std::function manages memory automatically, including capturing lambdas.
Limited to storing and calling functions, cannot store state.Can store state in addition to functions (e.g., storing lambdas or functors with state).
Used primarily for simple function calls and callbacks.Used when you need to store, pass, or return any callable object (functions, lambdas, functors).


Next Article
Practice Tags :

Similar Reads