Callback (computer Programming) - Use

Use

Callbacks have a wide variety of uses. For example, imagine a function that reads a configuration file and associates values with options. If the options are identified by a hash, then writing the function so that it takes a callback makes it more flexible: its user can choose whatever hashing algorithm is desired and the function will continue to work, since it uses the callback to turn option names into hashes; thus, callbacks allow the user of a function to fine-tune it at runtime. Another use is in error signaling. A Unix program, for example, might not want to terminate immediately when it receives SIGTERM; to make sure things get taken care of, it would register the cleanup function as a callback.

Callbacks may also be used to control whether a function acts or not: Xlib allows custom predicates to be specified to determine whether a program wishes to handle an event. The following code in C demonstrates the use of callbacks to display two numbers.

#include #include /* The calling function takes a single callback as a parameter. */ void PrintTwoNumbers(int (*numberSource)(void)) { printf("%d and %d\n", numberSource, numberSource); } /* A possible callback */ int overNineThousand(void) { return (rand % 1000) + 9001; } /* Another possible callback. */ int meaningOfLife(void) { return 42; } /* Here we call PrintTwoNumbers with three different callbacks. */ int main(void) { PrintTwoNumbers(&rand); PrintTwoNumbers(&overNineThousand); PrintTwoNumbers(&meaningOfLife); return 0; }

This should provide output similar to:

125185 and 89187225 9084 and 9441 42 and 42

Note how this is different from simply passing the output of the callback function to the calling function, PrintTwoNumbers - rather than printing the same value twice, the PrintTwoNumbers calls the callback as many times as it requires. This is one of the two main advantages of callbacks.

The other advantage is that the calling function can pass whatever parameters it wishes to the called functions (not shown in the above example). This allows correct information hiding: the code that passes a callback to a calling function does not need to know the parameter values that will be passed to the function. If it only passed the return value, then the parameters would need to be exposed publicly.

Another example:

/* * This is a simple C program to demonstrate the usage of callbacks * The callback function is in the same file as the calling code. * The callback function can later be put into external library like * e.g. a shared object to increase flexibility. * */ #include #include #include typedef struct _MyMsg { int appId; char msgbody; } MyMsg; void myfunc(MyMsg *msg) { if (strlen(msg->msgbody) > 0 ) printf("App Id = %d \n Msg = %s \n",msg->appId, msg->msgbody); else printf("App Id = %d \n Msg = No Msg\n",msg->appId); } /* * Prototype declaration */ void (*callback)(void *); int main(void) { MyMsg msg1; msg1.appId = 100; strcpy(msg1.msgbody, "This is a test\n"); /* * Assign the address of the function 'myfunc' to the function * pointer 'callback' */ callback = (void *)myfunc; /* * Call the function */ callback((MyMsg*)&msg1); return 0; }

The output after compilation:

$ gcc cbtest.c $ ./a.out App Id = 100 Msg = This is a test

This information hiding means that callbacks can be used when communicating between processes or threads, or through serialised communications and tabular data.

Read more about this topic:  Callback (computer Programming)

Famous quotes containing the word use:

    ... it is use, and use alone, which leads one of us, tolerably trained to recognize any criterion of grace or any sense of the fitness of things, to tolerate ... the styles of dress to which we are more or less conforming every day of our lives. Fifty years hence they will seem to us as uncultivated as the nose-rings of the Hottentot seem today.
    Elizabeth Stuart Phelps (1844–1911)