Pointer to Function and Callback Funtion

A function pointer(Pointer to Function/Subroutine Pointer/Procedure Pointer) is nothing else than a variable (stores the address of a function within executable memory) that can later be called through that function pointer. For instance, every time you need a particular behavior such as drawing a line, instead of writing out a bunch of code, all you need to do is call the function. But sometimes you would like to choose different behaviors at different times in essentially the same piece of code.

Functions in C are actually just pointers to a spot in the program where some code exists. Just like you've been creating pointers to structs, strings, and arrays, you can point a pointer at a function too. The main use for this is to pass "callbacks" to other functions, or to simulate classes and objects. This is useful because functions encapsulate behavior.

// define a function pointer
int (*ptr2Function)(int, char);

In this example, ptr2Function is a pointer to a function taking two argument, an integer and a character which returns void. It's as if you're declaring a function called "*ptr2Function", which takes an int and a char and returns void; now, if *ptr2Function is a function, then ptr2Function must be a pointer to a function. (Similarly, a declaration like int *x can be read as *x is an int, so x must be a pointer to an int.) 

Now lets start with reading part that how to read any function pointer declaration having more stars :

void *(*ptr2Function)(int *);

Here, the key is to read inside-out; notice that the innermost element of the expression is *ptr2Function, and that otherwise it looks like a normal function declaration. *ptr2Function should refer to a function that returns a void * and takes an int *. Consequently, ptr2Function is a pointer to just such a function.


Initializing Function Pointers

To initialize a function pointer, you must give it the address of a function in your program. The syntax is like any other variable:
#include <stdio.h>
void my_int_func(int x)
{
    printf( "%d\n", x );
}


int main()
{
    void (*ptr2Function)(int);
    /* the ampersand is actually optional */
    ptr2Function = &my_int_func;

    return 0;
}


Using a Function Pointer

To call the function pointed to by a function pointer, you treat the function pointer as though it were the name of the function you wish to call. The act of calling it performs the dereference; there's no need to do it yourself:
#include <stdio.h>
void my_int_func(int x)
{
    printf( "%d\n", x );
}


int main()
{
    void (*ptr2Function)(int);
    ptr2Function = &my_int_func;

    /* call my_int_func (note that you do not need to write (*ptr2Function)(2) ) */
    ptr2Function( 2 );
    /* but if you want to, you may */
    (*ptr2Function)( 2 );

    return 0;
}

Output :
2

Note that function pointer syntax is flexible; it can either look like most other uses of pointers, with & and *, or you may omit that part of syntax. This is similar to how arrays are treated, where a bare array decays to a pointer, but you may also prefix the array with & to request its address.


Callback Function :  

A function that is called through a function pointer. If you pass the pointer (address) of a function as an argument to another, when that pointer is used to call the function it points to it is said that a call back is made.

Now in programming way, callback is a piece of executable code that is passed as an argument to other code, which is expected to call back (execute) the argument at some convenient time. 

No comments:

Post a Comment