Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a callback? What is it for and how is it implemented in for example C++

I realise this is a newbie question, but as I'm trying to learn C++ I'm stumpling upon this expression "callback" frequently. I've googled it and checked wikipedia, but without finding a good explination. I am familiar with some Java and C#, but how unlikely it sounds, I have never really understood what a callback means.

If someone know how to explain this term to a simple layman, I would be really thankfull.

like image 618
Tomas Vinter Avatar asked Aug 12 '09 11:08

Tomas Vinter


People also ask

How are callbacks implemented in C?

Callbacks in C are usually implemented using function pointers and an associated data pointer. You pass your function on_event() and data pointers to a framework function watch_events() (for example). When an event happens, your function is called with your data and some event-specific data.

What is a callback and how do you use it?

A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action. The above example is a synchronous callback, as it is executed immediately.

How is call back function implemented?

A custom callback function can be created by using the callback keyword as the last parameter. It can then be invoked by calling the callback() function at the end of the function. The typeof operator is optionally used to check if the argument passed is actually a function. console.

Why do we need callback functions in C?

Callback functions are an essential and often critical concept that developers need to create drivers or custom libraries. A callback function is a reference to executable code that is passed as an argument to other code that allows a lower-level software layer to call a function defined in a higher-level layer(10).


2 Answers

I am familiar with some Java and C#

A callback is an event or delegate in those languages - a way to get your code run by somebody else's code in it's context. Hence, the term "callback":

  1. You call some other piece of code
  2. It runs, perhaps calculating an intermediate value
  3. It calls back into your code, perhaps giving you that intermediate value
  4. It continues running, eventually passing control back to you by completing

A canonical example is a sort routine with a user defined comparison function (the callback). Given a sort routine such as:

void Sort(void* values, int length, int valueSize, 
          int (*compare)(const void*, const void*) 
{
   for (int i = 0; i < length; i = i + 2) {
      // call the callback to determine order
      int isHigher = compare(values[i], values[i + 1]);
      /* Sort */
   }
}

(The specifics of how the sort is performed isn't important - just focus on the fact that any sorting algorithm needs to compare 2 values and determine which is higher.)

So, now we can define some comparison functions:

int CompareInts(const void* o, const void* p) {
   int* a = (int*) o;
   int* b = (int*) p;

   if (a == b) return 0;
   return (a < b) ? -1 : 1;
}

int ComparePersons(const void* o, const void* p) {
   Person* a = (Person*) o;
   Person* b = (Person*) p;

   if (a == b) return 0;
   return (a->Value() < b=>Value()) ? -1 : 1;
}

And reuse the same sort function with them:

int intValues[10];
Person personValues[10];

Sort(intValues, 10, sizeof(intVaues[0]), CompareInts);
Sort(personValues, 10, sizeof(personVaues[0]), ComparePersons);

Things get a bit more complicated if you're using member functions, as you have to manage the this pointer - but the concept is the same. As with most things, it's easier to explain them in C first. ;)

like image 91
Mark Brackett Avatar answered Oct 20 '22 21:10

Mark Brackett


In the simplest terms a callback is a code that you pass into another method.

E.g. you have a class A which calls a method on class B but you need some code running from Class A when it's finished. You put your code in its own new method on class A and you pass the method name in when you call the method on class B. When the method on class B has done its stuff it can 'call back' into class A.

Nowadays, you don't actually need to put the callback code in its own method: you've got anonymous methods and lambda expressions you can use. I think it's probably least confusing (at least, in C#) to learn using anonymous methods until you get it.

Good luck!

P.S. I was the same: I was coding C# for years before I really understood them.

like image 28
serialhobbyist Avatar answered Oct 20 '22 21:10

serialhobbyist