Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use a callback instead of a normal function call?

I'm trying to understand callbacks, and do get the idea, but do not understand why it is really needed.

Specifically, what added benefit does it provide over a normal function call? I'm looking at the accepted answer here : What is a "callback" in C and how are they implemented?

I have redone the same thing below, just without using function pointers. How is that different from this?

void populate_array(int *array, size_t arraySize)
{
   for (size_t i=0; i<arraySize; i++)
      array[i] = getNextRandomValue();
}

int getNextRandomValue(void)
{
   return rand();
}

int main(void)
{
   int myarray[10];
   populate_array(myarray, 10);
   ...
}

Is it only beneficial if lower-layer software needs to call a function that was defined at a higher-layer?

like image 276
Optimized Coder Avatar asked May 12 '13 10:05

Optimized Coder


People also ask

Why would you use a callback function?

A callback's primary purpose is to execute code in response to an event. These events might be user-initiated, such as mouse clicks or typing. With a callback, you may instruct your application to "execute this code every time the user clicks a key on the keyboard." button.

How does callback function differ from normal function call?

The main difference between a normal function and a callback function can be summarized as follows: A normal function is called directly, while a callback function is initially only defined. The function is only called and executed once a specific event has occurred.

Why do we use callback instead of promise?

They can handle multiple asynchronous operations easily and provide better error handling than callbacks and events. In other words also, we may say that, promises are the ideal choice for handling multiple callbacks at the same time, thus avoiding the undesired callback hell situation.

What's the difference between callback and function?

A callback is a function passed as an argument of another function. This means that the parent function is usually built to use any kind of function. But the callback function, on the other hand, is meant to be used in a specific case (or a restricted number of cases) in which the parent function is used.


2 Answers

This implementation is different in that the only way it knows to populate the array is hard-coded: getNextRandomValue is the way it populates the array, and it is part of the library that provides the populate_array functionality. Essentially, the two functions are baked together into a single whole (the fancy way of saying the same thing is to say that they are "tightly coupled").

With the original implementation I can do all of the things below without changing a line in the populate_array:

int getNextRandomValue(void) {
    return rand();
}
int getNextEvenValue(void) {
    static int even = 2;
    return (even += 2);
}
int getNextNegativeValue(void) {
    static int neg = -1;
    return neg--;
}
int getNextValueFromUser(void) {
    int val;
    scanf("%d", &val);
    return val;
}

populate_array(myarray, 10, getNextRandomValue);
populate_array(myarray, 10, getNextEvenValue);
populate_array(myarray, 10, getNextNegativeValue);
populate_array(myarray, 10, getNextValueFromUser);

With your implementation I would have to write a separate populate_array for each of these cases. If the way of populating an array that I need is not part of the library, I am on the hook for implementing the whole populate_array, not only its value-producing generator function. This may not be that bad in this case, but in cases where callbacks really shine (threads, asynchronous communications) reimplementing the original without callbacks would be prohibitive.

like image 141
Sergey Kalinichenko Avatar answered Oct 03 '22 00:10

Sergey Kalinichenko


In your example, getNextRandomValue must be known at compile time. This means that you cannot reuse the function with different definitions of getNextRandomValue, neither let other users link to your code and specify their version of getNextRandomValue. Note that this is not a callback, rather a "generic" function: think of getNextRandomValue as a placeholder for a function.

Callbacks are a special use of "generic" functions: they are called upon completion of an asynchronous task, to notify the user of the operation results (or progress).

I don't think that function pointers only suit low-to-high-level communication. Function pointers, as said, allow you to build generic code in C. A quick example, qsort(): order sorting needs a function that, given a and b, tells whether a>b, a<b or a==b, so that a and b can be placed in the right order. Well, qsort let you provide your own definition for order, thus offering a general, reusable piece of code.

like image 30
Stefano Sanfilippo Avatar answered Oct 03 '22 00:10

Stefano Sanfilippo