Home All about Callback Function

Latest reviews

Callback Function in PHP and C++

All about Callback Function


Callback function is an executable code normally used in programming languages for computers.

It is sort of an argument passed on to other codes. Callback function allows software layer at lower level to call a function (normally referred to as subroutine) which is predefined in the software layer of the higher level.

One of the best examples of callback functions are the function pointers like writing the sort function with qsort. The sort function is used to list items in a field in a specific order as required by the user.

Any type of items can make up the field, which is then passed through the sort function with the help of void-pointer.

Apart from the items themselves, the function also needs information on total number of items present and size of each item.

But if the type of the item is not specified, how can the sort function successfully sort the items in the required format? It is actually quite simple.

When the function obtains the pointer of a comparison-function and the void-pointers are taken to the specified field items, it evaluates the ranking of each item and returns the result coding it as an “int”. Thus when the sort algorithm has to decide on the ranking of any 2 items, it calls the comparison-function using the function pointer. This is what makes a “callback”

Callbacks can be used for a variety of purposes. For instance if there is a function of reading a configuration file and associate the options with values, where options are referred by hashes, then by including a callback function you can make the program more flexible.

It can be programmed such that the user can use different hashing algorithm and yet the function will work smoothly.

This is essentially because the callback function will make the program turn the names of the option into hashes thus allowing the user to fine tune the function at runtime.

The other best use of callback function is in error signaling. In a Unix program, to care of things when a termination order is given through SIGTERM, a cleanup function can be registered as a callback function so that valuable information is not lost. Callbacks are also used to determine if a function should work or not.

PHP Callback Function


These are structures which “point” to object methods, global functions and static methods in a class allowing in PHP before 5.3 a certain level of functional programming. The three classifications are

1. $cb1 = 'someGlobalFunction';
2. $cb2 = array(&$obj, 'somePublicMethod'); and
3. $cb3 = array('ClassName', 'someStaticMethod');

How to Implement Callbacks in C and C++?


Following the sample example as above :

void qsort(void* field, size_t nElements, size_t sizeOfAnElement,

int(_USERVALUE *cmpFunc)(const void*, const void*));

Here field is the first element of the “to be sorted” field, nElements is number of items in the specified field, sizeOfAnElement is size of the item in bytes and cmpFunc is the comparison function’s pointer.

The comparison function evaluates 2 void pointers and returns the result as an int.

The programming of a callback function is similar to the normal function calls; the only difference being the name of the function pointer is used instead of a function name as explained below.

void qsort( ... , int(_USERVALUE *cmpFunc)(const void*, const void*))

{

/* sort algorithm - note: item1 and item2 are void-pointers */

int bigger=cmpFunc(item1, item2); // make callback

/* use the result */

}
Similarly in C++, you implement the callbacks as in C functions. There is no requirement to invoke an object in the static member functions and hence the same signature used in a C function can be used with the same calling arguments, calling conventions and return type.