CPP Pointers to Functions

Pointers to Functions?

  • It is possible to declare a pointer variable that points to a function and
  • to invoke the function by using that pointer.
  • The syntax for creating a such pointer is:
    returnType (* functionPointerName) (param1Type, param2Type, param3Type ...);
    If we want a pointer to function, functionPointer, that takes one parameter as a double and returns a double we must create it as:
    double (* functionPoint) (double);
  • Pointers to Functions dates from the days of C, before object-oriented programming was available.
  • If you are writing a program that is highly dynamic and needs to operate different functionality based on runtime decisions, this can be a viable solution.
    A Full Program Example:
    #include <iostream>
    using namespace std;
    // Declare some function to calculate
    // area of figures
    double TriangleArea (double , double);
    double RectangleArea (double , double);
    double CircleArea (double , double);
    
    int main() {
      // create a pointer to Function, pointerFunc, that takes 
      // two double parameters and return a double
       double (* pointerFunc) (double, double);
       bool quit = false;
       double value1 =6;
       double value2 =5;
       int choice;
       while (quit == false) {
          cout <<  "(0)Quit (1)Triangle (2)Rectangle (3)Circle: ";
          cin >> choice;
          switch (choice){
              case 1: pointerFunc = TriangleArea; value2= 6; break;
              case 2: pointerFunc = RectangleArea; value2=5; break;
              case 3: pointerFunc = CircleArea; value2=3.14; break;
              default: quit = true; break;
          }
          if (quit == false) {
            // Using selected function to calculate
            // the area of selected figure
             double result= pointerFunc(value1, value2);
             cout << "Area of selected figure: " << result << "\n";
          }
       }
       return 0;
    }
    // Implementation of the function that calculates the 
    // area of a Triangle
    double TriangleArea (double width, double height) {
      return width*height/2;
    }
    // Implementation of the function that calculates the 
    // area of a Rectangle
    double RectangleArea (double width, double height) {
      return width*height;
    }
    // Implementation of the function that calculates the 
    // area of a Circle
    double CircleArea (double radius, double PI) {
      return radius*radius*PI;
    }
    When we run this application, the result will be:
    (0)Quit (1)Triangle (2)Rectangle (3)Circle: 1
    Area of selected figure: 18
    (0)Quit (1)Triangle (2)Rectangle (3)Circle: 2
    Area of selected figure: 30
    (0)Quit (1)Triangle (2)Rectangle (3)Circle: 3
    Area of selected figure: 113.04
    (0)Quit (1)Triangle (2)Rectangle (3)Circle: 0

    You can download this example here (needed tools can be found in the right menu on this page).

Passing a pointer to function to other functions?

  • You can pass Pointers to Function as parameter to other Functions.
    Example:
    #include <iostream>
    using namespace std;
    // Declare some function to calculate
    // area of figures
    double TriangleArea (double , double);
    double RectangleArea (double , double);
    double CircleArea (double , double);
    // Declare the showValues function
    void showValues(double (*) (double, double), double, double);
    
    int main() {
      // create a pointer to Function, pointerFunc, that takes 
      // two double parameters and return a double
      bool quit = false;
      double value1 =6;
      double value2 =5;
      int choice;
      while (quit == false) {
        cout <<  "(0)Quit (1)Triangle (2)Rectangle (3)Circle: ";
        cin >> choice;
        switch (choice){
              case 1: value2= 6;
                showValues(TriangleArea, value1, value2);
                break;
              case 2: value2=5;
                showValues(RectangleArea, value1, value2);
                break;
              case 3: value2=3.14;
                showValues(CircleArea, value1, value2);
                break;
              default: quit = true; break;
        }
      }
      return 0;
    }
    // A function to handle the pointer to function
    void showValues(double (* pointerFunc) (double, double), double p1, double p2){
      double result= pointerFunc(p1, p2);
      cout << "Area of selected figure: " << result << "\n";
    }
    
    // Implementation of the function that calculates the 
    // area of a Triangle
    double TriangleArea (double width, double height) {
      return width*height/2;
    }
    // Implementation of the function that calculates the 
    // area of a Rectangle
    double RectangleArea (double width, double height) {
      return width*height;
    }
    // Implementation of the function that calculates the 
    // area of a Circle
    double CircleArea (double radius, double PI) {
      return radius*radius*PI;
    }
    When we run this application, the result will be:
    (0)Quit (1)Triangle (2)Rectangle (3)Circle: 1
    Area of selected figure: 18
    (0)Quit (1)Triangle (2)Rectangle (3)Circle: 2
    Area of selected figure: 30
    (0)Quit (1)Triangle (2)Rectangle (3)Circle: 3
    Area of selected figure: 113.04
    (0)Quit (1)Triangle (2)Rectangle (3)Circle: 0

    You can download this example here (needed tools can be found in the right menu on this page).

  • It is may be best to typedef the pointer to function to get a more readable and clean code:
    // Typedef the pointer to function
    typedef  double (*PFUNC) (double, idouble)
    
    // Using the typedef in creating a pointer to function variable 
    PFUNC pFunc;
    . . . .
    // Using the typedef in creating a function
    // that takes a parameter of a pointer to function  
    void showValues( PFUNC pointerFunc, double p1, double p2){
      double result= pointerFunc(p1, p2);
      cout << "Area of selected figure: " << result << "\n";
    }
© 2010 by Finnesand Data. All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the correctness in the pages or source codes.
The risk of using this web-site pages or any program codes from this website is entirely at the individual user.