Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong><em>What is a Callbak function?</em></strong><br> In simple terms, a <strong>Callback function</strong> is one that is not called explicitly by the programmer. Instead, there is some mechanism that continually waits for events to occur, and it will call selected functions in response to particular events.<br> This mechanism is typically used when a operation(function) can take long time for execution and the caller of the function does not want to wait till the operation is complete, but does wish to be intimated of the outcome of the operation. Typically, Callback functions help implement such an <strong><em>asynchronous mechanism</em></strong>, wherein the caller registers to get inimated about the result of the time consuming processing and continuous other operations while at a later point of time, the caller gets informed of the result.</p> <p><strong><em>An practical example:</em></strong><br> Windows event processing:<br> virtually all windows programs set up an event loop, that makes the program respond to particular events (eg button presses, selecting a check box, window getting focus) by calling a function. The handy thing is that the programmer can specify what function gets called when (say) a particular button is pressed, even though it is not possible to specify when the button will be pressed. The function that is called is referred to as a callback.</p> <p><strong><em>An source Code Illustration:</em></strong></p> <pre><code>//warning: Mind compiled code, intended to illustrate the mechanism #include &lt;map&gt; typedef void (*Callback)(); std::map&lt;int, Callback&gt; callback_map; void RegisterCallback(int event, Callback function) { callback_map[event] = function; } bool finished = false; int GetNextEvent() { static int i = 0; ++i; if (i == 5) finished = false; } void EventProcessor() { int event; while (!finished) { event = GetNextEvent(); std::map&lt;int, Callback&gt;::const_iterator it = callback_map.find(event); if (it != callback_map.end()) // if a callback is registered for event { Callback function = *it; if (function) { (*function)(); } else { std::cout &lt;&lt; "No callback found\n"; } } } } void Cat() { std::cout &lt;&lt; "Cat\n"; } void Dog() { std::cout &lt;&lt; "Dog\n"; } void Bird() { std::cout &lt;&lt; "Bird\n"; } int main() { RegisterCallBack(1, Cat); RegisterCallback(2, Dog); RegisterCallback(3, Cat); RegisterCallback(4, Bird); RegisterCallback(5, Cat); EventProcessor(); return 0; } </code></pre> <p>The above would output the following: </p> <pre><code>Cat Dog Cat Bird Cat </code></pre> <p>Hope this helps! </p> <p><sup> <strong>Note:</strong> This is from one of my previous answers, <strong><a href="https://stackoverflow.com/questions/6183847/callback-functions-in-c-c-c/6184236#6184236">here</a></strong> </sup></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload