Main Page | See live article | Alphabetical index

Callback

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

C Example

Here is an example in the C programming language

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

/** print number in hexadecimal */ void printHex(int i) { printf("%x\\n",i); }

/** print number in decimal */ void printDec(int i) { printf("%d\\n",i); }

int main(void) { void (*fn)(int); printf ("decimal:\\n") fn = &printDec; counter(fn); printf ("hexa:\\n") fn = &printHex; counter(fn);

return 0 }

Example result

decimal:
15
16
17
18
19
hexa:
f
10
11
12
13
14