C Language Callback Function Detailed Explanation
Category Programming Techniques
1. What is a Callback Function?
The term "callback function" sounds more sophisticated than an ordinary function just by its name. But what exactly is a callback function? I must admit that I haven't read much and haven't found a definition of a callback function in any book. I searched on Baidu and found a variety of opinions. A large part of them use a similar scenario to illustrate: Mr. A goes to Mr. B's shop to buy something, but it happens to be out of stock. Mr. A leaves his phone number with Mr. B, and Mr. B will notify Mr. A when the goods are available. This scenario seems to be more about asynchronous operations rather than callbacks. In addition, there are two English sentences that left a deep impression on me: 1) If you call me, I will call you back; 2) Don't call me, I will call you. It seems to make sense, but upon closer examination, can't ordinary functions also achieve these two points? Therefore, I think these explanations are not quite appropriate because they do not express the characteristics of callback functions, that is, they do not show the difference between callback functions and ordinary functions. However, I think the analysis from Baidu Baike is quite good (although I often make fun of Baidu search...): A callback function is a function that is called through a function pointer. If you pass the pointer (address) of a function as an argument to another function, and this pointer is used to call the function it points to, we say this is a callback function.
Let me first share my views. We can first decompose it literally. For "callback function," Chinese can actually be understood in these two ways: 1) The function that is called back; 2) The function that performs the calling action in return. So, what is this return call?
Let's first look at the analysis of callback (Callback) from Wikipedia: 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. That is to say, passing a piece of executable code as an argument to other code, and this code will be called and executed at some point, is called a callback. If the code is executed immediately, it is called a synchronous callback, and if it is executed later at some point, it is called an asynchronous callback. I will not discuss synchronous and asynchronous here, please refer to relevant materials.
Let's take a look at a concise and clear statement from a Stack Overflow expert: A "callback" is any function that is called by another function which takes the first function as a parameter. That is to say, when function F1 calls function F2, function F1 passes another function F3's pointer to function F2 through an argument. During the execution of function F2, function F2 calls function F3. This action is called a callback (Callback), and the function F3 that is first passed as a pointer and then called back is the callback function. Now you should understand the definition of a callback function, right?
2. Why Use Callback Functions?
Many friends may wonder, why not just write the function name directly at the callback location like an ordinary function call? Can't that be done? Why must we use a callback function? This is a good question, because I have seen many examples on the internet that explain callback functions, which can actually be implemented with ordinary function calls. To answer this question, let's first understand the benefits and functions of callback functions, which is decoupling, yes, such a simple answer, it is because of this characteristic that ordinary functions cannot replace callback functions. Therefore, in my eyes, this is the biggest feature of callback functions. Let's take a look at a well-drawn picture on Wikipedia.
Below is an incomplete C language code to present the meaning of the above picture:
Example
#include<stdio.h>
#include<softwareLib.h> // Include the header file of the 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 function call between functions, and there is no difference from ordinary function calls. But upon closer inspection, you can find a key difference between the two: in the callback, the main program passes the callback function as an argument to the library function. In this way, as long as