When use callback function?
-
Hello all, When use callback function, and what is the purpose? Which cases we should use the callback functions? Could you tell me about? Thanks!
-
Hello all, When use callback function, and what is the purpose? Which cases we should use the callback functions? Could you tell me about? Thanks!
Callbacks are typically used when the function needs to notify its caller for some reason. This can be for simply informing the caller or for getting information necessary for the function to continue execution. For example, you can have a timer function that runs in a loop and needs to notify someone whenever a timer expires. You'd write something like
void TimerFunc(CallbackFunc *func)
{
while (1)
{
// Wait for specified time
...
func();
}
}They can also be used to alter the function's flow depending on the caller. For example
void SomeFunc(CallbackFunc* func)
{
int x = ...; // Some runtime parameter
if (func(x) == true)
{
// Do this
}
}As you can see, func gets the ability to change SomeFunc's control flow. Regards Senthil _____________________________ My Blog | My Articles | WinMacro