In C, a callback function is basically a function or executable code that is passed as an argument to another function.
The receiving function can then "call back" or run the the provided callback function at a given time ( on occurrence of certain events or conditions..Ex interrupts).
The Callback functions are commonly used to implement event handling mechanisms, asynchronous programming and to provide extensibility or customization points in libraries.
In most instances,a Callback will contain three Steps:
• The Callback function
• A Callback Registration
• Callback execution
Example of Callback Function,
In most instances,a Callback will contain three Steps:
• The Callback function
• A Callback Registration
• Callback execution
Example of Callback Function,
#include<stdio.h>
void PrintMessage(void);
void RunCallbackFun(void(*funptr)(void));
int main(void)
{
//Address of the Function PrintMessagesg() is passed a parameter to function RunCallBackFun()
RunCallBackFun(&PrintMessage);
return 0;
}
void PrintMessage()
{
printf("Hello World");
}
//This function takes in a function as a parameter
void RunCallBack(void(*fptr)(void))
{
(*fptr)();
}
Output:
Hello World
Advantages of using Callback Functions:
1. Dynamic Behavior: Callbacks enable dynamic behavior in your code. Depending on the callback function provided, the behavior of the calling function can change dynamically.
2. Event Handling: Callbacks are commonly used in event-driven programming. For example, in GUI applications, register callback functions to handle events like button clicks, mouse movements, etc.
3. Asynchronous Programming: Callbacks are used to handle asynchronous operations. For instance, in networking, you might provide a callback function to be executed once data is received from a remote server.
4. Customization: Libraries and frameworks often allow users to customize their behavior by providing callback functions. This enables developers to tailor the library to their specific needs without modifying its source code.
5. Decoupling: Callbacks help in decoupling different parts of the code. The caller doesn't need to know the specifics of the callback implementation; it just provides a function with the expected signature.
void PrintMessage(void);
void RunCallbackFun(void(*funptr)(void));
int main(void)
{
//Address of the Function PrintMessagesg() is passed a parameter to function RunCallBackFun()
RunCallBackFun(&PrintMessage);
return 0;
}
void PrintMessage()
{
printf("Hello World");
}
//This function takes in a function as a parameter
void RunCallBack(void(*fptr)(void))
{
(*fptr)();
}
Output:
Hello World
Advantages of using Callback Functions:
1. Dynamic Behavior: Callbacks enable dynamic behavior in your code. Depending on the callback function provided, the behavior of the calling function can change dynamically.
2. Event Handling: Callbacks are commonly used in event-driven programming. For example, in GUI applications, register callback functions to handle events like button clicks, mouse movements, etc.
3. Asynchronous Programming: Callbacks are used to handle asynchronous operations. For instance, in networking, you might provide a callback function to be executed once data is received from a remote server.
4. Customization: Libraries and frameworks often allow users to customize their behavior by providing callback functions. This enables developers to tailor the library to their specific needs without modifying its source code.
5. Decoupling: Callbacks help in decoupling different parts of the code. The caller doesn't need to know the specifics of the callback implementation; it just provides a function with the expected signature.