How to Create a Pointer to a Function in C++? Last Updated : 04 Apr, 2024 Comments Improve Suggest changes Like Article Like Report In C++, a function pointer is a variable that stores the address of a function that can later be called through that function pointer. It is useful for passing functions as parameters to other functions(callback functions) or storing them in data structures. In this article, we will learn how to use a pointer to a function in C++. Pointer to a Function in C++A pointer to a function is declared by specifying the function's return type, followed by an asterisk (*) inside parenthesis, the pointer name, and the parameter types of the function in parentheses. Declaring a Pointer to a Function in C++return_type(*pointer_name)(parameter_list)Here, return_type denotes the type of value returned by the function.pointer_name denotes the name of the pointer to a function.parameter_list denotes the list of parameters the function can accept.The parenthesis around the pointer_name is necessary, otherwise, it will be treated as a function declaration with the return type of return_type* and name pointer_name; Initializing a Pointer to a Function in C++We can initialize a pointer to a function by assigning it the address of a function with a matching signature. pointer_name = function_name; // Or &function_nameHere, function_name is the name of the function the pointer is associated with. Note: The address-of operator (&) is optional when initializing function pointers because the function name decays to a pointer to the function. C++ Program to Create a Pointer to a Function The following program illustrates how we can create and use a pointer to a function in C++. C++ // C++ Program to illustrate how we can create and use a // pointer to a function #include <iostream> using namespace std; // A simple function to be pointed to void display() { cout << "Hello Geek, Welcome to gfg!" << endl; } int main() { // Declare a function pointer void (*func_ptr)(); // Point it to the 'display' function func_ptr = display; // Call the function through the function pointer (*func_ptr)(); return 0; } OutputHello Geek, Welcome to gfg! Time Complexity: O(1)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article How to Create a Pointer to a Function in C++? gaurav472 Follow Improve Article Tags : C++ Programs C++ cpp-pointer CPP-Functions CPP Examples +1 More Practice Tags : CPP Similar Reads How to Return a Pointer from a Function in C++? In C++, we can return a pointer from a function which is useful when we want to return large data structures that cannot be returned by value. However, it must be done carefully to avoid memory leaks, dangling pointers, and other issues related to dynamic memory management. In this article, we will 2 min read How to Create a Function Template in C++? In C++, templates enable us to write generic programs that handle any data type. We can create a template class, function, and variable. A template function is a function that can work with any data type. In this article, we will learn how to create a function template in C++. Create a Function Temp 2 min read How to Create a Pure Virtual Function in C++? In C++, pure virtual functions are those functions that are not implemented in the base class. They are instead implemented in the derived classes if necessary. In this article, we will discuss how to create a pure virtual function in a class in C++. How to Create a Pure Virtual Function in C++? To 2 min read How to Create a Smart Pointer in C++? A smart pointer in C++ simulates a pointer while also providing automatic garbage collection as it deallocates or frees associated memory when it goes out of scope, which helps prevent memory leaks and dangling pointers. In this article, we will learn how to create a smart pointer in C++. Creating a 4 min read How to Return a Vector From a Function in C++? In C++, by returning vectors, users to return multiple values at once from a function as the elements of vector. In this article, we will learn how to return a vector from a function in C++.The recommended way to return a vector from a function is by using return vector by value method. Letâs take a 3 min read How to Declare a Static Member Function in a Class in C++? In C++, static functions are functions that are directly associated with a class so we can access the static function directly without creating an object of the class using the scope resolution operator. In this article, we will learn how we can declare a static function in a class in C++. Declare a 1 min read Returning a function pointer from a function in C/C++ In C/ C++, like normal data pointers(int *, char *, etc), there can be pointers to functions. Every function created in a program gets an address in memory since pointers can be used in C/C++, so a pointer to a function can also be created. Syntax: return type (*function_pointer_name) (argument_type 6 min read How to Pass an Array Pointer as a Function Argument in C++? In C++, a common situation arises where we have to pass an array to a function to perform some processing. This can be done by passing an array pointer as a function argument. In this article, we will learn how to pass an array pointer as a function argument in C++. Passing Array Pointer to Function 2 min read How to Create a shared_ptr in C++? A std::shared_pointer is a smart pointer introduced in C++11 that manages the lifetime of a dynamically allocated object through reference counting. In this article, we will learn how to create a shared_pointer. shared_ptr in C++A std::shared_pointer can be created by using the std::make_shared meth 2 min read How to call function within function in C or C++ When we begin programming in C/C++, we generally write one main() function and write all our logic inside this. This approach is fine for very small programs, but as the program size grows, this become unmanageable. So we use functions. We write code in the form of functions. The main function alway 4 min read Like