The callback function is a function that the programmer cannot explicitly call; by transmitting the address of the callback function to the caller, it is called. The callback function is necessary, and we want to achieve different content through a unified interface. At this time, it is very suitable for the return function. For example, we have written different display functions for several different devices: void TVShow (); void computershow (); void notebookshow () ... and more. This is what we want to use a unified display function, we can use the backfunction at this time. Void Show (VOID (* PTR) ()); call different callback functions according to the parameters of the incoming parameters.
Different programming languages may have different syntax, and the following is an example of a callback function in a C language. One of the callback functions does not with parameters, and the other callback function belt parameters.
Example 1:
//Test.c
#include
INT test1 () {INT i; for (i = 0; i <30; i ) {printf ("THE% D TH Charactor IS:% C / N", I, (Char) ('A' I% 26 );}}} Int test2 {INT i; for (i = 0; i Void Caller1 (VOID (* PTR) ()) // Pointer to function parameter {(* PTR) ();} void caller2 (int N, int (* ptr) ()) // Pointer to function Function parameters, here the first parameter is a pointer to the function, {// cannot be written as Void Caller2 (INT N)), such definition syntax errors. (* PTR) (N); Return;} int main () { Printf ("************************* / n"); Caller1 (TEST1); // is equivalent to calling TEST2 (); Printf ("&&&&&&& ************************ / N "); Caller2 (30, test2); // is equivalent to calling TEST2 (30); return 0;} The above is transmitted by transmitting the address of the callback function to the caller, but it should be noted that the usage of the reference adjustment function is required. To achieve a callback, you must first define a function pointer. The definition of the function pointer is slightly mentioned. such as: INT (* ptr) (); here PTR is a function pointer, wherein the parentheses of (* PTR) cannot be omitted, because the priority of parentheses is higher than the asterisk, which has become a return type as an integer declaration. Zhangggdlt 2005.04.25