Introduction to C and C++ callback functions

  • 2020-04-02 01:46:15
  • OfStack

For many beginners, callbacks are often mysterious and you want to know how they work. This article will explain what callback functions are, what they are good for, why they are used, and so on.

What is a callback function?

In short, a callback function is a function called through a function pointer. If you pass a function's pointer (address) as an argument to another function, we say it is a callback function when the pointer is used to call the function it points to.

Why use callback functions?

Because you can separate the caller from the caller. The caller doesn't care who is being called, all it needs to know is that there is a called function with a certain stereotype and certain constraints (such as an int return value).

If you want to know what's in the callback function in the actual work, to assume that there is a first case, we will write a library, it provides some sorting algorithms, such as bubble sort, quick sort, shell, shake, etc., but to make the library more general, don't want to be embedded in the function sequence logic, and allow users to implement the corresponding logic; Or, what if you want the library to be available for multiple data types (int, float, string)? Function Pointers can be used and callbacks made.

Callbacks can be used for notification mechanisms, for example, sometimes a timer is set in the program, and the program is notified at a certain time, but the implementer of the notification mechanism knows nothing about our program. At this point, we need to have a pointer to a function with a specific stereotype, which is used to make a callback to inform us that the program event has occurred. In fact, the SetTimer() API USES a callback function to notify the timer and, in case no callback function is provided, it sends a message to the program's message queue.

Another API function that USES a callback mechanism is EnumWindow(), which enumerates all the top-level Windows on the screen, calls a program-provided function for each window, and passes the window's handler. If the caller returns a value, continue the iteration; otherwise, exit. EnumWindow() doesn't care where the caller is or what the caller does with the handler it passes; it only CARES about the return value, because based on the return value, it will proceed or exit.

Anyway, callbacks continue to be from C, so in C++, callbacks should only be used if you have an interface with C code or an existing callback interface. In addition to the above, a virtual method or functor should be used in C++, not a callback function.

Here's a simple callback function I wrote myself, which is easier to understand than other complex code:


#include<stdio.h>
#include<stdlib.h>
void perfect(int n)
{
 int i=1;
    int count=0;
 for(i=1;i<n;i++)
 {

  if(0==n%i)
  {
   count+=i;
  }
 }
 if(count==n)
  printf("%d Is the number of n",n);
 else printf("%d Not the number of n",n);
}
void myCallback(void (*perfect)(int ),int n)
{
 perfect(n);
}
int main()
{
 int n;
 printf(" Please enter a positive integer n");
 scanf("%d",&n);
 myCallback(perfect,n);
 return 0;

}


Related articles: