Broad Network


Pointer to Function in C++

C++ Just After the Basics - Part 6

Forward: In this article, I explain Pointer to Function in C++.

By: Chrysanthus Date Published: 22 Aug 2012

Introduction

When talking about pointers in C++, you are dealing with two objects: the pointer object and the pointed object. The pointer object has the address of the pointed object. The pointer object is what is called the pointer. When you type a function in C++ the normal way, that function is a piece of code in memory. This piece of code has a starting address. You can have an object whose content would be this starting address of the function code. This object is a pointer to the function. In this article, I explain Pointer to Function in C++.

You need basic knowledge in C++ in order to understand this article. If you do not have that knowledge, then type, “Getting Started with C++” and my name Chrys in the search box of this page and click Search. That will take you to a series I wrote on C++ basics.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

Ordinary Function
In the following program, the function is an ordinary function:

#include <iostream>
using namespace std;

float myFn(float ident1, int ident2)
    {
        float difference = ident1 - ident2;
        return difference;
    }

int main()
    {
        float answer = myFn(6.5, 2);
        cout << answer;

        return 0;
    }

In this program, myFn() is an ordinary function. We can make an object point to it.

Pointers and Return Values
Consider the following code segment:

    float hisFloat;
    float *myPointer = &hisFloat;

The first statement above declares an object, which the second statement turns into a pointed object. In the second statement, myPointer is a pointer object (by initialization). Its type specifier is float. There is no type specifier as pointer or any other similar name. When a pointer is declared, its type specifier is the type of value held by the pointed object. A type specifier, by its position, in the above code segment, looks like a return type in function declaration.

So, when declaring a pointer to a function, you also have to write the return type of the function the pointer will point to. A pointer to a function has its own way of initialization (declaration). (see below).

Pointer to Function Syntax
The syntax for the initialization of a pointer to a function is,

    Type (*functionPointer)(parameterTypes) = actualFunctionName;

Here, Type is the return type of the function the pointer will point to. functionPointer is the identifier of the object that will have the address of the start code of the function. ParameterTypes are the parameter types of the function pointed to. The parentheses and the asterisk are required.

Assume that you want a pointer to the myFn() function above with the name, myFnPtr. In this case the pointer initialization is,

    float (*myFnPtr)(float, int) = myFn;

Now, the name of a function (e.g. myFn) is actually a pointer to a function. So, functionPointer and actualFunctionName become synonymous. In our case, myFnPtr and myFn become synonymous.

Calling Pointed Function by Pointer
As functionPointer and actualFunctionName are synonymous, you can call the actual function using functionPointer, in the same way as you would use actualFunctionName. In the situation above, you would do, for example,

    myFnPtr(68.78, 50)

Example
The following code shows how you can create a pointer to a function and use it to call the function:

#include <iostream>
using namespace std;

float myFn(float ident1, int ident2)
    {
        float difference = ident1 - ident2;
        return difference;
    }

float (*myFnPtr)(float, int) = myFn;

int main()
    {
        float answer = myFnPtr(6.5, 2);
        cout << answer;

        return 0;
    }

Pointer by Declaration
In the above examples the pointer has been created as,

float (*myFnPtr)(float, int) = myFn;

This is creating pointer by initialization. In the statement the address of the pointed function is stored in the pointer object (myFnPtr), during creation of the pointer. You can create the pointer by declaration and then assign the address value later as follows:

        float (*myFnPtr)(float, int);
        myFnPtr = myFn;

In this case, both statements or the assignment statement should be in a function (like the main function).

Use of Pointer to a Function
The primary use of a pointer to a function is as a parameter to another function. The following code illustrates this:

#include <iostream>
using namespace std;

float myFn(float ident1, int ident2)
    {
        float difference = ident1 - ident2;
        return difference;
    }

float squareDifference(float (*myFnPtr)(float, int))
    {
        float sqr = myFnPtr(6.5, 2) * myFnPtr(6.5, 2);
        return sqr;
    }

int main()
    {
        float answer = squareDifference(myFn);
        cout << answer;

        return 0;
    }

The secret is that you split the initialization into its left and right operand. The left operand goes into the parentheses of the other function (squareDifference) as a parameter, while the right operand is used as argument in the calling of the other function. What we have done here is to pass a pointer to a function same as what happens with pointers of fundamental objects. The parameter of the function (squareDifference) is the pointer declaration while the argument of the calling function is the memory address that the pointer object would have as value. The difference here is that the pointer declaration is complex and it points to a function. Read the above code if you have not already done so.

The other function can actually have more than one parameter (argument); just separate the parameters with commas, even if some of the parameters are pointers to functions.

The above code does not look very useful, but using a pointer to a function as a parameter for another function can actually become complicated. I will not go into any of that complication. However, you now know what is a pointer to a function and how to use a pointer to a function as parameter to another function (split the initialization into two; left operand becomes a parameter for the other function and right operand becomes an argument in the call of the other function). The good news is that the mystery of pointer to function is now demystified.

That is what I have prepared. We continue in the next part of the series.

Chrys

Related Courses

C++ Course
Relational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
NEXT

Comments

Become the Writer's Fan
Send the Writer a Message