Definition of a callback function

33 views
Skip to first unread message

david allen

unread,
Aug 30, 2018, 1:57:19 PM8/30/18
to fltk.general
That a function is passed as an argument to another function sufficient to call it a callback function? Or, is it also required that the function must be evoked in response to some signal or event?

Ian MacArthur

unread,
Aug 30, 2018, 4:24:23 PM8/30/18
to fltkg...@googlegroups.com


> On 30 Aug 2018, at 18:57, david allen wrote:
>
> That a function is passed as an argument to another function sufficient to call it a callback function? Or, is it also required that the function must be evoked in response to some signal or event?


I don’t think I know the answer to that... I was inclined towards your latter description but Wikipedia seems closer to the first, I quote:

"In computer programming, a callback, also known as a "call-after" function, is any executable code that is passed as an argument to other code, which is expected to call back (execute) the argument at a given time. This execution may be immediate as in a synchronous callback, or it might happen at a later time as in an asynchronous callback. In all cases, the intention is to specify a function or subroutine as an entity[clarification needed] that is, depending on the language, more or less similar to a variable (see first-class functions).

Programming languages support callbacks in different ways, often implementing them with subroutines, lambda expressions, blocks, or function pointers.”

Quote from: https://en.wikipedia.org/wiki/Callback_(computer_programming)


That said, I’m still not sure!


Matthias Melcher

unread,
Aug 31, 2018, 11:44:32 AM8/31/18
to fltkg...@googlegroups.com


> On Aug 30, 2018, at 19:57, david allen <david...@uky.edu> wrote:
>
> That a function is passed as an argument to another function sufficient to call it a callback function? Or, is it also required that the function must be evoked in response to some signal or event?

Passing functions is one thing. You can pass functions as arguments any time. In C/C++, "passing a function" is a nice way of saying, "here is a pointer to something, I hope you know what you are doing". C/C++ only "knows" that something is a pointer to a function during compile time. After that, it's just a pointer. That's why function pointers can be used in arrays, as class members, or whatever other case you think of.

In FLTK specifically, function pointers are used exclusively in callbacks. Callbacks are a way to defer execution of code to a later point in time. You know the code that needs be run when a button is pressed, but you don't want to run it now, but later, when the button is actually pressed, so you set it as a callback.

Not all callbacks are directly related to user interaction. There are timer callbacks that are called after some time elapsed, or idle callbacks, that are called whenever there is nothing else to do.

FLTK uses callbacks for everything that is needed for a basic User Interface. The idea in UI programming is, that anything can happen at any time. So when constructing a UI, you give FLTK all those callbacks, so that FLTK can call any of those at any time, depending on what the user does.

The other method is to override the handle() method, but that is an advanced topic and less of a UI user thing, more a library developer item.

Greg Ercolano

unread,
Aug 31, 2018, 12:19:15 PM8/31/18
to fltkg...@googlegroups.com
The latter.

Passing a function to another function/method is just "passing a function pointer".

A "callback" is typically a pre-arranged function to be called when something
happens, so yes, a signal, event. In the case of FLTK, typically a hardware event
like a keyboard or mouse/tablet click/drag, but can also be a timer going off,
and other such things.

david allen

unread,
Aug 31, 2018, 1:14:48 PM8/31/18
to fltk.general
Thanks for the replies. I understand the terminology now!
Reply all
Reply to author
Forward
0 new messages