”;
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 −
void hello(){ 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> void hello(){ printf("Hello Worldn"); } void callback(void (*ptr)()){ printf("Calling a function with its pointern"); (*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().
int square(int val){ return val*val; } int root(int val){ return pow(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.
int callback(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> int callback(int a, int (*print_callback)(int)); int square(int value); int root (int value); int main(){ int x = 4; printf("Square of x: %d is %dn", x, callback(x, square)); printf("Square root of x: %d is %dn", x, callback(x, root)); return 0; } int callback(int a, int (*ptr)(int)){ int ret = (*ptr)(a); return ret; } int square(int val){ return val*val; } int root(int val){ return pow(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.
”;