Callback function (callback)

Author: Huang Jing to
link: https: //www.zhihu.com/question/19801131/answer/17156023
Source: know almost
copyrighted by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
 

The word callback was originally used for making calls. You can call (call) to others, or you can leave a phone number to let others call back (callback). The computer field is relatively new, and some everyday vocabulary has been introduced to express similar concepts. Call and callback are translated into "call" and "callback" in the computer field.

A callback function is a function you write and call by the pre-written system. You call the system function, it is straightforward. Let the system call your function, which is a callback. But if you are satisfied with this one-sentence conclusion, you will not really understand.

--------------------------------

The callback function can be regarded as the extra information passed in to let others do something.

A lets B do things, depending on the granularity, it can be understood as A function calls B function, or A uses B class, or A component uses B component and so on. Anyway, A asks B to do something.

When B does this, he does not need enough information, and A has it. It is necessary for A to pass in from outside, or B to do it before applying from outside. For B, one is to get information passively, and the other is to get information actively. Some people give these two methods a term called push of information (push) and pull of information (pull).

A calls B, and A needs to pass parameters to B.

The following function takes the maximum value of two numbers, and then passes in a and b. This is easy to understand.

int max(int a, int b); 

The following function is used for sorting, and the last parameter is a callback function, which defines a certain behavior so that qsort can pull information from the inside.

void qsort(void *, size_t, size_t, int (*)(const void *, const void *));

But this parameter seems to be more difficult to understand, why? I think it is an artificial separation of code and data.

--------------------------------

Let us pause for a moment, take a step back and look at the weird parts of the computer, that is, the unification of code and data. This is a threshold. If you don’t cross this threshold, many concepts will be unclear. We often The computer program is divided into two parts: code and data. Many people will understand that code can run and is dynamic, and data is used by code and is static. These are two completely different things.

In fact, code is just a description of behavior. For example, a robot can turn on lights, turn off lights, and sweep the floor. If agreed with the robot, 0 means turn on the light, 1 means turn off the light, and 2 means sweep the floor. I issue a string of commands, 0 1 2, to control the robot to turn on the lights, turn off the lights, and sweep the floor. It is agreed to use binary representation, two instructions, there is a string of numbers, 000111. At this time, the string of numbers 000111 describes a series of actions of the robot. This is understood from one aspect as a code, which can control the behavior of the robot. But on the other hand, it can be transferred, can be recorded, can be modified, that is, data. As long as everyone negotiates, the code can be encoded into data, and when the data is interpreted and run, it also becomes code.

Code and data can be referred to as information without distinction. Since int max (int a, int b) int, double and other things that represent ordinary data can be passed in, naturally the function that represents code can also be passed in. Some languages ​​are indeed indistinguishable. Its function (representing code) has the same status as int and double. In this language, functions are first-class values.

And some languages ​​cannot store functions, cannot dynamically create functions, and cannot dynamically destroy functions. Only one pointer to a function can be stored. This language called a function is the second type of value.

In addition, some languages ​​can not only transfer functions, but also use some external information (including code, data) in the functions. Those languages ​​can transfer and store functions together with the information used by the function. This kind of function and the information it uses as a whole is called a closure.

After this threshold, unify the code and data, many incomprehensible concepts will become clearer.

------------------

Now let's look back at the callback function. The callback function is that A asks B to do something, and B is doing it, and the information is not enough, and if you don't know how to do it, let the outside process.

For example, in the above sorting example, A sorts B, and B will do the sorting, but the sorting needs to know which is larger than which is. If B doesn't know this, A needs to tell it. And judging the size itself is a certain behavior. Since the function of the first value cannot be passed in the C language, it is designed to pass the function pointer of the second value. This function pointer is the information passed from A to B to describe the size of the judgment this behavior. Here originally, A called B, and as a result, B called the information that A told it, which is the callback.

Another example is that A asks B to monitor a certain message of the system, such as which key is pressed. Then B listened to it, but it didn’t know how to process the message, so it would be handled by people who care about the message and how to process the message. The process itself is an act. If the language cannot pass functions, it’s Only one function pointer can be passed. If the system stores the function pointer, it can be called anytime later. Code and data are information, data can be stored, and functions used to express behavior can naturally also be stored.

Afterwards, some people will come to the conclusion, such as registration, notification, etc. If B is monitoring, C, D, E, F, G, H tell B that he is interested in knowing the news, then B will tell C, D, E, F, G, etc., so many people will be notified , It's called broadcast.

A term for an abstract concept can be clearly aware of the existence of this concept, which is helpful for learning and understanding. But after understanding, you don’t need to care too much about terminology. On different occasions, the same concept often has different terms.

Then generalize the concept of callback, for example, someone cares about A, B, C, D, E, F events at the same time, and these events are a group, such as keyboard knocking, mouse movement, mouse click, etc. Combine a group of events. In some languages, it is mapped to an interface, which has N functions. Some languages ​​are mapped into a structure with N function pointers inside. Then, instead of passing in a single function pointer, it passes in the interface or the structure of the function pointer. According to different purposes, some people call it an agent, listener, observer, etc.

In fact, a certain behavior is also stored and called later when necessary. It is no different from the callback function at a deeper level.

Guess you like

Origin blog.csdn.net/sempre20/article/details/112708961