The best programs are written so that computing machines can perform them quickly and so that human beings can understand them clearly. A programmer is ideally an essayist who works with traditional aesthetic and literary forms as
well as mathematical concepts, to communicate the way that an algorithm works and to convince a reader that the results will be correct. Donald E. Knuth

Callbacks in Computer Programming

Callback Functions are a very important and useful concept in computer programming. Callback function is passed as an argument to some other function which is expected to call it at some point of execution. In simple words, suppose we pass a callback function X( ) to some other function Y( ) as an argument. Then Y( ) is supposed to call X( ) at some point.
Callback mechanism is implemented in C and C++ using Function Pointers. If you are not familiar with function pointers or just want to brush up the knowledge, please have a look at Function Pointer Concept .

Let's understand callbacks using a simple example of sort function. We will implement a program which can sort an array of integers in both ascending and descending order.

/*
 * This program demonstrates the usage of callback function.
 * We implement a Selection sort function which uses a callback function
 * to determine whether the array is to be sorted in ascending or 
 * descending order.
 */
#include<iostream>
using namespace std;

// Display the Array
void displayArray(int arr[], int size) {
   for (int i = 0; i < size; i++) {
      cout << arr[i] << " ";
   }
}

// checks if first argument is greater than second argument
bool isGreater(int x, int y) {
   return (x > y);
}

// checks if first argument is smaller than second argument
bool isSmaller(int x, int y) {
   return (x < y);
}

// swap elements in the array
void swap(int arr[], int idx1, int idx2) {
   int temp = arr[idx1];
   arr[idx1] = arr[idx2];
   arr[idx2] = temp;
}

/* selection sort
 * select the min element at each iteration and store them successively
 * bool (*compare)(int, int) is the callback function
 */
void selectionSort(int arr[], int size, bool (*compare)(int, int)) {
   int i,j;
   int min_idx;
   for (i = 0; i < (size - 1); i++) {
      // select the minimum element and store it at 'i'th index
      min_idx = i;
      for (j = i + 1; j < size; j++) {
         if (compare(arr[min_idx], arr[j])) {
            min_idx = j; // update the min index
         }
      }
      if (min_idx != i) {
         // put the min element in the index pointed by 'i'
         swap(arr, min_idx, i);
      }
   }
}

// main
int main() {
   int arr[] = { 7, 4, 5, 2, 3, 19, 11, 6, 9, 12, 8 };
   int size = sizeof(arr) / sizeof(arr[0]);
   cout<<"\nOriginal Array :: ";
   displayArray(arr,size);

   /*
    * we want the array to be sorted in ascending order
    * call the 'sort' function with 'isGreater' as callback function
    */
   selectionSort(arr, size, &isGreater);
   cout<<"\nSorted Array (Ascending Order)  :: ";
   displayArray(arr,size);

   /*
    * we want the array to be sorted in descending order
    * call the 'sort' function with 'isSmaller' as callback function
    */
   selectionSort(arr, size, &isSmaller);
   cout<<"\nSorted Array (Descending Order)  :: ";
   displayArray(arr,size);

   cout<<endl;
   return 0;
}

We can observe that function selectionSort( ) is called with a callback function isGreater( ) as one of the arguments and then this function is called by selectionSort( ) function.

Types of Callback

Synchronous Callback : This type of callback is also known as blocking callback because the callback function is invoked and completely executed before the calling function returns i.e the calling function can return only after the callback function returns. The program shown above is an example of synchronous callback.

Asynchronous Callback : In this type, calling function can return after invoking the callback function. The callback function can return at any point of time. This type of callbacks are implemented in multi-threaded applications where the calling function and the callback function may be executing in different threads.