What is the difference between a callback function and a normal function?

Source: Internet
Author: User
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
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.