Callback functions are extremely versatile, particularly in event-driven programming. When a specific event is triggered, a callback function mapped to it is executed in response to these events. This is typically used in GUI applications, an action like a button click can initiate a series of predefined actions.
Callback Function
The callback function is basically any executable code that is passed as an argument to other code, that is expected to call back or execute the argument at a given time. We can define it in other words like this: If the reference of a function is passed to another function argument for calling, then it is called a callback function.
The mechanism of callbacks depends on function pointers. A function pointer is a variable that stores the memory address of a function.
Here is a simple hello() function in C −
voidhello(){printf("Hello World.");}
We declare a pointer to this function as follows −
void(*ptr)()=&hello;
We can now call the function with the help of this function pointer, (*ptr)();
Example of Callback Function in C
In this example, the hello() function is defined as an argument to myfunction().
#include <stdio.h>voidhello(){printf("Hello World\n");}voidcallback(void(*ptr)()){printf("Calling a function with its pointer\n");(*ptr)();// calling the callback function}main(){void(*ptr)()= hello;callback(ptr);}
Output
Calling a function with its pointer
Hello World
Callback Function With Arguments
In the example given below, we have also declared two functions with identical prototypes − square() and root().
intsquare(int val){return val*val;}introot(int val){returnpow(val,0.5);}
The callback function is defined to receive an argument as well as a function pointer with an integer argument that matches with the above functions.
intcallback(int a,int(*ptr)(int)){int ret =(*ptr)(a);return ret;}
In the main() function, we place a call to the callback by passing an integer and the name of the function (square / root) which becomes the function pointer in callback’s definition.
Example of Callback Function With Arguments
The complete code is as follows −
#include <stdio.h>#include <math.h>intcallback(int a,int(*print_callback)(int));intsquare(int value);introot(int value);intmain(){int x =4;printf("Square of x: %d is %d\n", x,callback(x, square));printf("Square root of x: %d is %d\n", x,callback(x, root));return0;}intcallback(int a,int(*ptr)(int)){int ret =(*ptr)(a);return ret;}intsquare(int val){return val*val;}introot(int val){returnpow(val,0.5);}
Output
Square of x: 4 is 16
Square root of x: 4 is 2
Types of Callbacks in C
There are two types of callbacks −
Synchronous Callback
A callback is synchronous when it is given to another function, which executes it as part of its process. The calling function waits for the callback to complete before proceeding. This is useful when you need immediate results or want to ensure a task is finished before moving on.
Asynchronous Callback
In this case, the calling function triggers the callback but doesn’t wait for it to finish. Instead, it continues its execution. It results in non-blocking operations. It’s commonly used in event-driven programming.
Generic callback functions help developers write C programs that are versatile and better adaptable.
In this chapter, we explained how you can use function pointers so that we can enhance the flexibility of our C programs. Additionally, we showed how you can create generic callback functions that are not limited to a specific function pointer type.
Leave a Reply