1. What is a callback function?
Callback functions are taller than ordinary functions just by their names. What is a callback function? I'm sorry I haven't read much, but I didn't see the definition of callback function in that book. I searched Baidu and found that there are different opinions. A large part of them use a similar scene to explain: when a Jun goes to B Jun's store to buy something, it happens to be out of stock. A Jun leaves a number to B Jun and notifies a Jun when it is available. It seems that asynchronous operation is easier to think of than callback. In addition, there are two English sentences that impress me: 1) If you call me, I will call you back; 2) Don't call me, I will call you. It seems very reasonable, but when you think about it carefully, can't ordinary functions do these two things? Therefore, I don't think such statements are very appropriate, because I don't think these statements express the characteristics of callback functions, that is, they can't see the difference between callback functions and ordinary functions. However, I think it's pretty good to analyze Baidu Encyclopedia (though I often make complaints about Baidu search): A callback function is a function called through a function pointer. If you pass the pointer (address) of a function as a parameter to another function, when the pointer is used to call the function it points to, we say it is a callback function.
Let's talk about my opinion first. We can literally decompose it first. For "callback function", Chinese can actually understand two meanings: 1) the callback function; 2) Go back to the function that called the action. What the hell is this call back?
In computer programming, a Callback is any executable code that is passed as an argument to other code, which is expected to call back (execute) the argument at a given time This execution may be immediate as in a synchronous Callback, or it might happen at a later time as in an asynchronous Callback. In other words, an executable code is passed to other code like a parameter, and this code will be called and executed at a certain time, which is called a Callback. If the code is executed immediately, it is called a synchronous Callback. If it is executed later, it is called an asynchronous Callback. Synchronous and asynchronous are not discussed here. Please refer to relevant materials.
A "callback" is any function that is called by another function which takes the first function as a parameter. That is, when function F1 calls function F2, function F1 passes the pointer of another function F3 to function F2 through parameters. During the execution of function F2, function F2 calls function F3. This action is called Callback, and the function F3 first passed as a pointer and then called back is the Callback function. Now you should understand the definition of Callback function?
2. Why use callback function?
Many friends may think, why not write the name of the function directly in the callback place like ordinary function calls? Isn't that ok? Why do I have to use callback functions? It's a good idea because many examples of parsing callback functions can be called with ordinary functions. To answer this question, let's first understand the benefits and functions of returning to functions, that is, decoupling. Yes, it's such a simple answer. Because of this feature, ordinary functions can't replace callback functions. Therefore, in my eyes, this is the biggest feature of callback functions. Let's take a look at a picture on Wikipedia that I think is well drawn.
The following is an incomplete C language code to show the meaning of the above figure:
example
#include<stdio.h> #include<softwareLib. h> / / contains the header file of the read Software library where the Library Function is located int Callback() // Callback Function { // TODO return 0; } int main() // Main program { // TODO Library(Callback); // TODO return 0; }
At first glance, the callback seems to be just a call between functions, which is no different from ordinary function calls, but a closer look shows a key difference between the two: in the callback, the main program passes the callback function into the function like a parameter. In this way, as long as we change the parameters passed into the library function, we can realize different functions. Do you think it is very flexible? And there is no need to modify the implementation of library functions, which is decoupling. Take a closer look. The main function and callback function are on the same layer, while the library function is on another layer. Think about it. If the library function is invisible to us, we can't modify the implementation of the library function, that is, we can't modify the library function to call ordinary functions, so we can only pass in different callback functions, This is a common situation in daily work. Now put the main(), Library(), and Callback() functions back into the F1, F2, and F3 functions. Is that more clear?
After understanding the characteristics of callback function, can you roughly know under what circumstances it should be used? Yes, you can use callback functions in many places instead of ordinary function calls, but in my opinion, callback functions should be used if you need to reduce the coupling.
3. How to use callback function?
If you know what a callback function is and the characteristics of a callback function, how should you use a callback function? Let's take a look at a simple executable synchronous callback function code.
example
#include<stdio.h> int Callback_1() // Callback Function 1 { printf("Hello, this is Callback_1 "); return 0; } int Callback_2() // Callback Function 2 { printf("Hello, this is Callback_2 "); return 0; } int Callback_3() // Callback Function 3 { printf("Hello, this is Callback_3 "); return 0; } int Handle(int (*Callback)()) { printf("Entering Handle Function. "); Callback(); printf("Leaving Handle Function. "); } int main() { printf("Entering Main Function. "); Handle(Callback_1); Handle(Callback_2); Handle(Callback_3); printf("Leaving Main Function. "); return 0; }
Operation results:
Entering Main Function. Entering Handle Function. Hello, this is Callback_1 Leaving Handle Function. Entering Handle Function. Hello, this is Callback_2 Leaving Handle Function. Entering Handle Function. Hello, this is Callback_3 Leaving Handle Function. Leaving Main Function.
You can see that the parameter in the Handle() function is a pointer. When calling the Handle() function in the main() function, the function callback is passed in to it_ 1()/Callback_ 2()/Callback_ 3 (), the function name at this time is the pointer of the corresponding function, that is, the callback function is actually a use of function pointer. Now read this sentence again: A "callback" is any function that is called by another function which takes the first function as a parameter?
4. How to use callback function with parameters?
Sharp eyed friends may find that the callback function in the previous example has no parameters, so can we call back those functions with parameters? The answer is yes. So how to call it? We can modify the above example slightly:
example
#include<stdio.h> int Callback_1(int x) // Callback Function 1 { printf("Hello, this is Callback_1: x = %d ", x); return 0; } int Callback_2(int x) // Callback Function 2 { printf("Hello, this is Callback_2: x = %d ", x); return 0; } int Callback_3(int x) // Callback Function 3 { printf("Hello, this is Callback_3: x = %d ", x); return 0; } int Handle(int y, int (*Callback)(int)) { printf("Entering Handle Function. "); Callback(y); printf("Leaving Handle Function. "); } int main() { int a = 2; int b = 4; int c = 6; printf("Entering Main Function. "); Handle(a, Callback_1); Handle(b, Callback_2); Handle(c, Callback_3); printf("Leaving Main Function. "); return 0; }
Operation results:
Entering Main Function. Entering Handle Function. Hello, this is Callback_1: x = 2 Leaving Handle Function. Entering Handle Function. Hello, this is Callback_2: x = 4 Leaving Handle Function. Entering Handle Function. Hello, this is Callback_3: x = 6 Leaving Handle Function. Leaving Main Function.
As you can see, instead of directly changing int Handle(int (*Callback)(int)) to int Handle(int (*Callback)(int)), you can add another parameter to save the parameter value of the callback function, such as the parameter Y of int Handle(int y, int (*Callback)(int)). Similarly, callback functions with multiple parameters can be used.