#### Archived

This topic is now archived and is closed to further replies.

# callback functions

This topic is 6150 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

would someone mind explaining what a callback function is/does?

##### Share on other sites
Hi.

A Callback-Function is a function that may be called by the operating system.

for example the Windows Procedures are CALLBACK.
And if Windows has a new Message for your window, it will send it to your windows windows procedure wich you defined in the wndclass, when you call
DispatchMessage(&message);

##### Share on other sites
Definition of callbak function is a little more general than those that can be called by OS. A good example of a generalized callback function is the Observer/Observable pattern (from Design Patterns book by Gamma et al).

##### Share on other sites
On a low level (like the robot I'm currently programming), a callback is used to jump from a point in code to a specific function then jump back to that point again once that function is complete.

Obviously, in a high level language, pretty much any function does this anyway, so I don't know how it fits in in 'normal' programming.

Catfish

Edited by - Catfish on March 17, 2001 4:04:17 PM

##### Share on other sites
A callback function is a function that you typically want to have executed either asynchronously from your normal program flow, or at some point when you might want to call a function without knowing what it is in advance.

One of the big uses of callback functions is in multithreaded applications. If you have one thread that is responsible for all control of an application''s graphics, and you have another thread that is responsible for controlling the application, an effect like combining a bitmap with the current contents of the screen in some non-standard manner isn''t always the easiest thing to do. For instance, suppose it''s in a program with a design that allows plugins. Then there''s simply no way to set up that functionality in the stuff the graphics thread knows about ahead of time, because at the time the code for the graphics thread is written, the plugin might not even exist.

So, the control thread might use a callback to deal with this situation. It would tell the graphics thread to call the blend function in the plugin, which could then take advantage of the fact that it is running in the graphics thread, meaning it would be assured of having access to the device context, and that the contents of the buffer that it was blending with wouldn''t change in the middle of the operation, as might happen if the blend function occurred in a different thread.

That was a kind of convoluted example, I suppose. There are simpler ones using timers (call this auto-save function every 3 minutes, asynchronous to normal flow of execution). But with those couple examples, you should get the idea of what a callback is for.

##### Share on other sites
The easiest example I can think of is when you want buttons in your program, the simplest way is to create the button with a callback, so when your GUI-system detects that the button has been pressed, it will call your (callback-)function.
Some pseudo-code:
  void MyCallback(){ printf("Button pressed!\n");}int main(){ guiAddButton("Press me", MyCallBack); guiMainloop();}

That way you''ll know what button was pressed and you can also do the appropriate action for it.
So callbacks are simply put, functions (or similar) that you pass to some other part of your software in good hope that that part will call it (without knowing anything about it...).

Hmm, I wonder if that made any sense...