Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • C++ Data Types
  • C++ Input/Output
  • C++ Arrays
  • C++ Pointers
  • C++ OOPs
  • C++ STL
  • C++ Interview Questions
  • C++ Programs
  • C++ Cheatsheet
  • C++ MCQ
  • C++ Projects
  • C++ Exception Handling
  • C++ Memory Management
Open In App
Next Article:
Function Pointer in C++
Next article icon

Function Pointer in C++

Last Updated : 27 Jan, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Prerequisites:

  • Pointers in C++
  • Function in C++

Pointers are symbolic representations of addresses. They enable programs to simulate call-by-reference as well as to create and manipulate dynamic data structures. Iterating over elements in arrays or other data structures is one of the main use of pointers.

The address of the variable you’re working with is assigned to the pointer variable that points to the same data type (such as an int or string).

Syntax:

datatype *var_name; 

Address of Function: We all know that every function's code resides in memory, so every function has an address like all other variables in the program. The name of a function can be used to find the address of the function. We can get the address of a function by just writing the function’s name without parentheses in the function.

To know more about this, refer to the article - address of the function.

Function Pointer in C++

  • The function pointer is used to point functions, similarly, the pointers are used to point variables. 
  • It is utilized to save a function's address. 
  • The function pointer is either used to call the function or it can be sent as an argument to another function.
Function Pointer in C++
Function Pointer in C++

Syntax: 

return_type (*FuncPtr) (parameter type, ....);  

Referencing and Dereferencing of the Function Pointer in C++

Similar to the pointer used with variables we perform referencing and dereferencing with a function pointer.

Referencing: When pointer is allocated the address of the function to be associated with it then this process is referred to as referencing.

Dereferencing: When we use the (*)operator  to get the value stored in the pointer.

Syntax:

// Declaring return_type (*FuncPtr) (parameter type, ....);   // Referencing FuncPtr= function_name;  // Dereferencing data_type x=*FuncPtr;

Function pointer used to call the function

In this, we see how we point a pointer to a function and call it using that pointer. It is an efficient way to use 

Example:

C++
// C++ program to implementation // Function Pointer #include <iostream> using namespace std;  int multiply(int a, int b) { return a * b; }  int main() {     int (*func)(int, int);      // func is pointing to the multiplyTwoValues function      func = multiply;      int prod = func(15, 2);     cout << "The value of the product is: " << prod << endl;      return 0; } 

Output
The value of the product is: 30

In the above program, we are declaring a function multiply where we are multiplying two elements a and b, then returning the result. But, rather than directly calling the function we are using a function pointer prod which is doing the same work for us.

Passing a function pointer as a parameter

When declaring a function pointer to store the memory address of the function but, when we want to pass the return value to the next function. We have two methods to perform this task. First, either pass the value we got or second pass the function pointer that already exists.

Example:

C++
// C++ Program for demonstrating // function pointer as pointer #include <iostream> using namespace std;  const int a = 15; const int b = 2;  // Function for Multiplication int multiply() { return a * b; }  // Function containing function pointer // as parameter void print(int (*funcptr)()) {     cout << "The value of the product is: " << funcptr()          << endl; }  // Driver Function int main() {     print(multiply);     return 0; } 

Output
The value of the product is: 30

Time complexity: O(1).
Auxiliary space: O(1).

In the above program, we are declaring a multiply function in which we multiply 2 variables a and b. We are passing the function pointer as a parameter in the print function, here we use the function pointer to calculate the value from the multiply function and then that value in the print function. 


Next Article
Function Pointer in C++

A

akashjha2671
Improve
Article Tags :
  • Technical Scripter
  • C++
  • Technical Scripter 2022
  • cpp-pointer
Practice Tags :
  • CPP

Similar Reads

    Function Pointer in C
    In C, a function pointer is a type of pointer that stores the address of a function, allowing functions to be passed as arguments and invoked dynamically. It is useful in techniques such as callback functions, event-driven programs, and polymorphism (a concept where a function or operator behaves di
    6 min read
    Inline Functions in C++
    In C++, inline functions provide a way to optimize the performance of the program by reducing the overhead related to a function call. When a function is specified as inline the whole code of the inline function is inserted or substituted at the point of its call during the compilation instead of us
    6 min read
    strol() function in C++
    The strtol() function in C++ interprets the contents of a string as an integral number of the specified base and return its value as a long int.This function also sets an end pointer that points to the first character after the last valid numeric character of the string, if there is no such characte
    3 min read
    std::function in C++
    The std::function() in C++ is a function wrapper class which can store and call any function or a callable object. In this article, we will learn about std::function in C++ and how to use it in different cases.Table of ContentWhat is std::function in C++?Example of std::functionMember Functions of s
    5 min read
    log() Function in C++
    The std::log() in C++ is a built-in function that is used to calculate the natural logarithm (base e) of a given number. The number can be of any data type i.e. int, double, float, long long. It is defined inside the <cmath> header file.In this article we will learn about how to use std::log()
    1 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences