Jump to content

Callback (computer programming)

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by 62.210.251.106 (talk) at 11:11, 15 December 2003 (stub). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

In computer science, a callback is a concept, which allow a low level layer to call a function from a hight level layer, when usually, the hight level call the low level layer.


Use

When is it used? (Concrete programs)

Languages

This should be don using languages such as th C language.

With object oriented languages, this should naturally be done throw an object, and inheritence.

Example

Example code

l.c

void counter ( void (*cb)(int) )
{
   int i;
   for (i=15; i<20;i++)
   {
       cb(i);
   }
}


h.c

printHex (int i)
{
   printf ("%x\n",i);
}
printDec (int i)
{
   printf ("%d\n",i);
}
int main ()
{
   counter (printDec);
   counter (printHex);
}


Example result

15
16
17
18
19
f
10
11
12
13