What is the difference between callback functions and ordinary functions?

怪我咯
Release: 2023-03-10 19:40:02
Original
3236 people have browsed it

Normal The difference between function and callback function:

Call to a normal function: After the calling program issues a call to the normal function , the program execution immediately switches to the called function until the called function completes execution, and then returns to the calling program to continue execution. From the perspective of the calling program, this process is "Call --> Wait for the called function to complete --> Continue execution"

Call the callback function: The calling program issues a call to the callback function After that, without waiting for the function to complete execution, it returns immediately and continues execution. In this way, the calling program and the called function are executed at the same time. When the called function completes execution, the called function will in turn call a pre-specified function to notify the calling program that the function call has ended. This process is called callback (Callback), which is where the name of the callback function comes from.

Ordinary functions and callback functions are called by other functions. The difference lies in whether the caller knows exactly what he is calling. For example:
void func1(){}
void func2(){}
typedef void (*fp)();//DeclarationFunction pointer, fp represents a null parameter and returns a function pointer of type void
void funcCaller1() {
//Here func1 can be regarded as a normal function
func1();//Here, funcCall1 clearly knows that it has called func1
}
void funcCaller2(fp funcPtr){
funcPtr();//Here, funcCall2 does not know what it is calling. It only knows that it has called a function with empty parameters and a return type of void
}
void funcCaller3(){
//Here, funcCaller2 is an ordinary function, func1 is a callback function
funcCaller2(func1);//Call funcCaller2, the parameter is func1
//Here, funcCaller2 is an ordinary function, func2 is a callback function
funcCaller2(func2);//Call funcCaller2, the parameter is func2
}

The above is the detailed content of What is the difference between callback functions and ordinary functions?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!