Callback function is a very important concept in program development. The so-called callback is actually the interface and convention between different program modules, which is the basic way of software layered design.
Callback is a very important mechanism, which can be used to realize the layered design of software, so that the work progress of developers of different software modules can be independent and not limited by time and space. When needed, they can fit together with each other through the agreed interface (or standard). This is what modern programming languages like C++ or JAVA call interface-oriented programming. At the same time, callback is also the cornerstone of customized software. Through callback mechanism, the front-end and back-end of the software are separated. The front-end provides the logical strategy, and the back-end provides the logical implementation.
Actually we know of the C language function is equivalent to a person, you give him some of the parameters, he help you to complete the same fixed function, let’s say you to cook a dish, he will be cooking this dish out, you give the doctor a patient, he will to heal, and so on, the patient here, cooks and doctors are equivalent to the exact function function, waiting for you to call. There are many such “functions” around us. Suppose you go to the canteen every day to eat, and slowly you find that it takes too long to walk back and forth from each meal, so you want to ask the receptionist to help your little sister when she eats. So, before the little sister goes out for lunch, you pass some parameters to her (for example: 10 yuan, and say what you want to eat), half an hour later, little sister with a hot lunch box back to you. This is the ordinary function call process: you call the little sister, realize your feet do not move hands can also eat the long-cherished wish, use the diagram to illustrate the process:
So what is a callback?
Eat in the canteen food after a week, really too difficult to eat, you already make sure that you can’t eat any longer, so you decide not to a canteen meal, to eat her own cooking, of course, you can’t personally, you still call the front desk little sister work for you, this time you must also give her some money, and you want, let her help you to vegetable market to buy back, side for you. Although the receptionist sister is very willing to serve you, she can run errands, buy vegetables, bargain prices, serve tea cups, but unfortunately she can not cook, you rushed to wisdom, decisively hired a veteran chef from the black market, what can not cook, you just give him materials. In this way, your lunch production process becomes: you will eat the list of dishes and need to spend the money passed to the front desk little sister, not only that, you also give the chef’s phone number to little sister, told: buy food to call this phone, let him help you to do the dishes, and then you work together to send me over. So the little sister pidian pidian ground went out to buy food, at the same time, you can “customize” your chef, you told your chef said: chef, I like to eat hot, in the future you do any dish, should put enough enough pepper. Please look at:
In the “lunch” show above, the realization of the typical callback mechanism, what do you do custom made a any dishes siming chillies chef, and then you call the front desk of little sister at the same time, the chef’s phone number to her too, little sister in need of cooking, can according to the parameters given by you (the chef’s phone) back to the past call (callback!) The chef, do a good meal after the little sister and then carefully ordered and ordered and ordered, served to you to eat.
You should also notice the most important piece of information in the picture above: there is a deep dotted line between the two people on the left and the one on the right, which means there are two groups of people on the left and right. The person on the left does the cooking and eating, and the person on the right does the running, haggling, and so on. Such as: as long as you know how to find the front desk, your little sister to her parameters to work to go, don’t need to know where she buy dish ran far way, the other mind chefs, let him who is being put pepper, also don’t need when GuanXiaoMei will go looking for chef, and any other things between them. As for the girl on the right, she just needs to run errands as you tell her and “call back” to the chef if necessary, regardless of the flavor of the meal.
Look, the two groups of people on the left and right are doing their own things, not coupled, strong independence, but can very easily cooperate with each other to achieve a richer function. Here, chef is a callback function — a function that is not called directly by the designer (you), but is called back by someone else (sister). The phone number you pass to your little sister is the equivalent of a pointer to the chef. This is called a function pointer, and the callback mechanism tells the location of the callback function by passing the function pointer.