Passing a function as a parameter in C++
In this tutorial, we are going to learn about passing a function as a parameter in C++.
A function is a block of code that is used for performing certain actions and runs only when they are called. We pass data, known as parameters in the function. They are important for reusing code and once defined we can use them as many times we want.
The general form of the function is given as-
return_type name_of_function([argument_1 type, argument_1 name]){ //Perform some operations }
Passing a function as a parameter is quite a useful concept in C/C++. Now, we will see different ways to create functions that accept another function as their parameter or arguments. We will use three approaches to do so-
Approach-1: Using pointers and passing them to functions
A pointer is a variable that stores the address of another variable. A function can be passed to another function as an argument by passing its address to that function. Let us look at the example below to understand the concept.
#include <iostream> using namespace std; // Function that sum two numbers int sum(int a, int b) { return a + b; } // Function that takes a pointer // to the function int pointer_arg(int a, int b, int (*function)(int, int)) { return function(a, b); } int main() { // Pass pointers for adding cout << "Sum of 30 and 20 = "; cout << pointer_arg(30, 20, &sum)<<endl; return 0; }
Output:
Sum of 30 and 20 = 50
Approach 2: Using std::function<>
In C++ there is a std::function<> template class that allows to pass functions as objects. This std::function can copy, store, invoke, and call any callable target– (function, lambda expression, bind expression, or any other function object. It can also call pointer to member function and data member as well. An object of std::function can be created as –
std::function<return type(argument_1 type, argument_2 type...)>object name //This object that is created //will be used to call the function as below return_type catch_variable= obj_name(argument_1,argument_2)
The code below explains the approach being discussed here.
#include <functional> #include <iostream> using namespace std; // Defining sum function //for adding two numbers int sum(int a, int b) { return a + b; } // Function that accepts an object of // type std::function<> as a parameter int std_invoke(int a, int b, function<int(int, int)> func) { return func(a, b); } int main() { // Pass the required function as parameter cout << "Sum of 30 and 20 = "; cout << std_invoke(30, 20, &sum); return 0; }
Output:
Sum of 30 and 20 = 50
Approach 3: Using lambdas
Lambda expressions in C++ allow us to write an inline function that can be used for short snippets of code that are not going to be reused and not worth naming. These are used for defining inline, one-time, anonymous function objects. Lambdas can be used in places where we want to pass a function as an argument.
Here, is the C++ program to illustrate the same.
#include <functional> #include <iostream> using namespace std; // Defining Function //that takes a pointer to a function int invoke_lambda(int a, int b, function<int(int, int)> func) { return func(a, b); } int main() { // Define lambdas for addition // where we want to pass another function // as an argument cout << "Sum of 30 and 20 is "; int x = invoke_lambda(30, 20, [](int a, int b) -> int { return a + b; }); cout << x << endl; return 0; }
Output:
Sum of 30 and 20 = 50
Thus, in this article, we have learned to pass functions as parameters using three different approaches.
Leave a Reply