Callback callback function introduction (C language)

Table of contents

1. Definition of callback function

2. Why use a callback function

3. How to use the callback function

3.1 How to use a callback function without parameters

3.2 How to use the callback function with parameters


1. Definition of callback function

Recently, the callback function callback is often used in work, so let’s summarize it.

Let’s take a look at Wikipedia’s explanation of callback: In computer programming, a callback is any executable code that is passed as an argument to other code, which is expected to call back (execute) the argument at a given time. This execution may be immediate as in a synchronous callback, or it might happen at a later time as in an asynchronous callback. Called to execute, this is called a callback. If the code is executed immediately it is called a synchronous callback, if it is executed at a later time then it is called an asynchronous callback.

Let’s take a look at the statement from a great god on Stack Overflow: A “callback” is any function that is called by another function which takes the first function as a parameter. That is, when function F1 calls function F2, function F1 calls function When F2, F1 passes the pointer of another function F3 to F2 through parameters, and the function F3 will be called during the execution of F2. This action is callback (callback), and the function F3 that is passed in as a pointer and then called back It is the callback function.

Finally, look at Baidu Encyclopedia's explanation: a callback function is a function called through a function pointer. If you pass a function pointer (address) as a parameter to another function, when this pointer is used to call the function it points to, we say this is a callback function.

2. Why use a callback function

Why can't we just write the name of the function in the place of the callback like a normal function? Why do you have to use a callback function? In fact, at some point the callback function can be implemented using ordinary functions, but the callback function still has its role. Among them, the biggest advantage of the callback function is decoupling, so ordinary functions cannot completely replace the callback function.

#include<stdio.h>
#include<softwareLib.h>
int Callback()           //Callback Function
{
    //TODO
    return 0;
}

int main()
{
    //TODO
    Library(Callback);
    //TODO
    return 0;
}

Take a closer look, in the callback function, the main program passes the callback function into the library function like a parameter. In this way, as long as the parameters passed into the library function are changed, different functions can be realized without modifying the implementation of the library function at all, which is decoupling.

In addition, the main function and the callback function are in the same layer, while the library function is in another layer. The library function is encapsulated in the library, and the implementation of the library function cannot be modified, that is to say, it cannot be implemented by modifying the library function to make the library function call an ordinary function, then we can only pass in different callback functions.

In fact, ordinary functions can be used instead of callback functions in many places, but if you need to reduce the degree of coupling, you should use callback functions.

3. How to use the callback function

3.1 How to use a callback function without parameters

First look at the callback function without parameters. The following is a simple executable synchronous callback function code.

#include<stdio.h>
int Callback_1()
{
    printf("Hello, this is Callback_1 \n");
    return 0;
}

int Callback_2()
{
    printf("Hello, this is Callback_2 \n");
    return 0;
}

int Callback_3()
{
    printf("Hello, this is Callback_3 \n");
    return 0;
}

int Handle(int (*Callback)())
{
    printf("Entering Handle Function.\n");
    Callback();
    printf("Leaving Handle Function. \n");
}

int main()
{
    printf("Entering Main Funtion. \n");
    Handle(Callback_1);
    Handle(Callback_2);
    Handle(Callback_3);
    printf("Leaving Main Function.\n");
    return 0;
}

The result of the operation is:

Entering Main Function.
Entering Handle Function.
Hello, this is Callback_1
Leaving Handle Function.
Entering Handle Function.
Hello, this is Callback_2
Leaving Handle Function.
Entering Handle Function.
Hello, this is Callback_3
Leaving Handle Function.
Leaving Main Function.

It can be seen that the parameter in the Hanle() function is a pointer. When the Handle() function is called in the main() function, the function pointer of Callback_1()/Callback_2()/Callback_3() is passed to it, that is That is to say, the callback function is actually a usage of function pointer. Read it again: "A "callback" is any function that is called by another function which takes the first function as a parameter." Is it more clear?

3.2 How to use the callback function with parameters

In the previous section, we learned how to use the callback function. What if the callback function needs input parameters? Of course you can use a callback function with parameters. Simply modify the example above:

 #include<stdio.h>

    int Callback_1(int x) // Callback Function 1
{
        printf("Hello, this is Callback_1: x = %d \n", x);
        return 0;
    }

    int Callback_2(int x) // Callback Function 2
{
        printf("Hello, this is Callback_2: x = %d \n", x);
        return 0;
    }

    int Callback_3(int x) // Callback Function 3
{
        printf("Hello, this is Callback_3: x = %d \n", x);
        return 0;
    }

    int Handle(int y, int (*Callback)(int))
{
        printf("Entering Handle Function. \n");
        Callback(y);
        printf("Leaving Handle Function. \n");
    }

    int main()
{
        int a = 2;
        int b = 4;
        int c = 6;
        printf("Entering Main Function. \n");
        Handle(a, Callback_1);
        Handle(b, Callback_2);
        Handle(c, Callback_3);
        printf("Leaving Main Function. \n");
        return 0;
    }

The result of the operation is:

Entering Main Function. 
Entering Handle Function. 
Hello, this is Callback_1: x = 2
Leaving Handle Function.
Entering Handle Function. 
Hello, this is Callback_2: x = 4
Leaving Handle Function.
Entering Handle Function.
Hello, this is Callback_3: x = 6
Leaving Handle Function.
Leaving Main Function.

As you can see, you only need to add a parameter to the callback function, such as int Callback_1(int x), and then add a parameter (int Handle(int y, int (*Callback)(int))) when calling, and pass it to the callback function. By the same token, you can also use a callback function with multiple parameters.

Guess you like

Origin blog.csdn.net/panpan_jiang1/article/details/126865422