What is the difference between an event handler and a callback function?
What is the difference between an event handler and a callback function?
A callback is procedure you pass as an argument to another procedure. The procedure receiving the parameter can call it, or share it so some other procedures in the system can call it.
An event handler is a procedure called when an event happens. It can be a callback.
Generally speaking, a 'callback' is under the control of the detecting process.
So you tell a GUI manager "call myaction when this button is pressed" and the GUI manager calls the action when the button is pressed.
Event Handlers on the other hand operate at one step removed. The GUI manager is configured to send messages to an event handler. You tell an event manager that button pushes are handled by the myaction program. When the button is pushed the GUI manager puts a message on the event handler's queue and gets on with GUI managing. The event handler picks up the message from the queue, sees it's a button push, fires up the myaction program, and moves on to handling the next event. Usually the myaction program will run as an independent thread or even a separate process.
While the "event handler" pattern is more complex it is much more robust and less likely to hang when an action fails. It also makes for a more responsive GUI.
An event handler is a type of callback. It's called whenever an event occurs. The term is usually used in terms of user interfaces where events are things like moving the mouse, clicking something and so on.
Callback (from Wikipedia): "executable code that is passed as an argument to other code".
Event handler (again from Wikipedia): "asynchronous callback subroutine that handles inputs received in a program".
Which happens to be the way I've always understood it: an event handler is a very specific type of callback.
A callback is function(method) you pass as an argument to another function(method). The function(method) receiving the parameter can call it, or share it so some other function(method) in the system can call it.
An event handler is a function(method) called when an event happens. It can be a callback.
Event callbacks and handlers, at the highest abstract level, are the same i.e. you have an event and a response to that event. (A response is just a piece of code that needs to run in response to something that happened.) The difference between the two lies in the fact that if this code is directly or indirectly invoked. In the case of callbacks, the code i.e. the callback itself is directly invoked. In the case of event handlers, this event is first communicated or notified to the right recipient and then from then on, the code is then invoked.
In interactive computer graphics, these terminologies are used in a bit different way than other fields. For example, if we are using a Graphics API like (let's say OpenGL), then Events are different actions on the window. The actions include window resizing, mouse motion, keyboard interaction etc. It is not necessary the events always need to be involved with physical, it can also be logical events such as programmers can rotate and move positions.
Callbacks (a.k.a. event handlers) are the functions to handle even mode input. For instance, in GLUT (OpenGL library)
glutMouseFunc(myMouse) -> here myMouse is a logical event, and that is being handles by glutMouseFunc() is the callback.
One extra thing to note (and perhaps the most important) is that, events can be handled by many consumers/receivers of the event.
While the callback implies a single receiver of the notification (well, unless you are in the JavaScript world were monkey-patching functions is a norm...).
So, callbacks are for more "private" communication between two parties, while events are for more "public announcements" and multiple subscribers.
Plus, as mentioned in another answer (see answer from supi), there is better decoupling in events. And more flexibility (e.g., you can usually remove event handlers whenever you want, while with callbacks sometimes it is not possible).
Callback is for asking another function to do business operations and send a result whereas events are not but asking for handover the control so that we can only handle business operation.
Eg: Button click is event (We are doing business operation on click of button). Looping collections in callback since forEach method is doing business operation and providing result.
An event has special features like bubbling and so on. If you do not need those features, then just use a callback.