The difference between a normal function and a callback function:
Call to the normal function: After the calling program makes a call to the normal function, the program executes immediately to the called function execution, until the called function finishes executing, and then returns to the calling program to continue execution. From the point of view of the calling program, the procedure is "call-and-wait for the called function to execute and continue."
Call to callback function: After the calling program makes a call to the callback function, it returns and continues execution immediately after the function is executed. In this way, the calling program executes concurrently with the called function. When the called function finishes executing, the called function invokes a pre-specified function to inform the calling program that the function call is finished. This process is called a callback (Callback), which is the origin of the callback function name.
Normal functions, callback functions are called by other functions, except that the caller knows exactly what they are calling, for example:
void Func1 () {}
void Func2 () {}
typedef void (*FP);//Declare function pointer, FP represents an empty argument, return function pointer of type void
void FuncCaller1 () {
Here func1 can be seen as a normal function
Func1 ();//Here, FuncCall1 clearly knows that he has called func1
}
void FuncCaller2 (FP funcptr) {
Funcptr ();//Here, FUNCCALL2 does not know what he is calling, only knows that he has called an empty argument, the return type is void function
}
void FuncCaller3 () {
Here, FuncCaller2 is the normal function, FUNC1 is the callback function
FuncCaller2 (func1);//Call FuncCaller2, parameter is func1
Here, FuncCaller2 is the normal function, FUNC2 is the callback function
FuncCaller2 (FUNC2);//Call FuncCaller2, parameter is FUNC2
}