Programmer's Wiki


407pages on
this wiki
Add New Page
Talk0 Share

A callback, function object, function pointer, or delegate is a value which represents a subroutine and is passed from one bit of code to another, in the process being assigned to a different variable. This is accomplished in different ways across languages:

Callbacks are used in cases where some of the logic is specified in user code, such as event-handling. Normally the callback is written within user code and passed as an argument to a library function, but sometimes a callback will instead be returned from a function for user code to call (or not) when appropriate.

For example, given the following filter function in pseudocode:

list filter(list l, delegate f)
    initialize list r
    for i = 0 to l.count - 1
        if f.invoke(l[i]) = true then r.add l[i]
    return r

We can put in any type of filtering method that we like to pass to the function. For example, given a function that will return true if the passed number is greater than 9:

boolean gr(int x)
    return x > 9

You can use this to get all numbers greater than 9 on the list:

// given a list somelist
delegate pfilter = reference to gr
list result = filter(somelist, pfilter)

Ad blocker interference detected!

Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.