Cprogramming 简明教程
Callback Function in C
回调函数非常灵活,特别是在事件驱动的编程中。当一个特定事件被触发时,将执行映射到它的一个回调函数以响应这些事件。这通常用于 GUI 应用程序,例如单击按钮这样的动作可以引发一系列预定义的 action。
Callback functions are extremely versatile, particularly in event-driven programming. When a specific event is triggered, a callback function mapped to it is executed in response to these events. This is typically used in GUI applications, an action like a button click can initiate a series of predefined actions.
Callback Function
回调函数基本上是作为参数传递给其他代码的任何可执行代码,该代码预计在给定的时间调用或执行该参数。我们可以用其他方式来定义它:如果一个函数的引用被传递给另一个函数参数进行调用,则它被称为回调函数。
The callback function is basically any executable code that is passed as an argument to other code, that is expected to call back or execute the argument at a given time. We can define it in other words like this: If the reference of a function is passed to another function argument for calling, then it is called a callback function.
The mechanism of callbacks depends on function pointers. A function pointer is a variable that stores the memory address of a function.
下面是在 C 中的一个简单的 hello() 函数:
Here is a simple hello() function in C −
void hello(){
printf("Hello World.");
}
我们按如下方式声明指向此函数的指针:
We declare a pointer to this function as follows −
void (*ptr)() = &hello;
现在,我们借助此函数指针,调用该函数{s0}
We can now call the function with the help of this function pointer, (*ptr)();
Example of Callback Function in C
此示例中,hello() 函数被定义为 myfunction() 的参数。
In this example, the hello() function is defined as an argument to myfunction().
#include <stdio.h>
void hello(){
printf("Hello World\n");
}
void callback(void (*ptr)()){
printf("Calling a function with its pointer\n");
(*ptr)(); // calling the callback function
}
main(){
void (*ptr)() = hello;
callback(ptr);
}
Callback Function With Arguments
下面提供的示例中,我们还声明了两个具有相同原型的函数——square() 和 root()。
In the example given below, we have also declared two functions with identical prototypes − square() and root().
int square(int val){
return val*val;
}
int root(int val){
return pow(val, 0.5);
}
回调函数被定义为接收一个数字和一个函数指针,带有与上一个函数匹配的数字参数。
The callback function is defined to receive an argument as well as a function pointer with an integer argument that matches with the above functions.
int callback(int a, int (*ptr)(int)){
int ret = (*ptr)(a);
return ret;
}
在 main() 函数中,我们通过传递一个数字以及 function({s1} / {s2})的名称来调用回调,这个名字在 callback 的定义中成为函数指针。
In the main() function, we place a call to the callback by passing an integer and the name of the function (square / root) which becomes the function pointer in callback’s definition.
Example of Callback Function With Arguments
完整代码如下所示:
The complete code is as follows −
#include <stdio.h>
#include <math.h>
int callback(int a, int (*print_callback)(int));
int square(int value);
int root (int value);
int main(){
int x = 4;
printf("Square of x: %d is %d\n", x, callback(x, square));
printf("Square root of x: %d is %d\n", x, callback(x, root));
return 0;
}
int callback(int a, int (*ptr)(int)){
int ret = (*ptr)(a);
return ret;
}
int square(int val){
return val*val;
}
int root(int val){
return pow(val, 0.5);
}
Square of x: 4 is 16
Square root of x: 4 is 2
Types of Callbacks in C
共有两种{s3} −
There are two types of callbacks −
Synchronous Callback
当回调被提供给另一个功能时,它才是同步的,该函数会将其作为其过程的一部分执行。调用函数会等待回调完成再继续执行。这在你需要立即结果或者想要确保任务完成再继续执行时很有用。
A callback is synchronous when it is given to another function, which executes it as part of its process. The calling function waits for the callback to complete before proceeding. This is useful when you need immediate results or want to ensure a task is finished before moving on.
Asynchronous Callback
这种情况下,调用函数会触发回调,但是不会等待它完成。相反,它将继续执行。它会导致非阻塞操作。它通常用于事件驱动的程序设计。
In this case, the calling function triggers the callback but doesn’t wait for it to finish. Instead, it continues its execution. It results in non-blocking operations. It’s commonly used in event-driven programming.
通用回调函数可以帮助开发者编写 C 程序,这些程序多功能、更具适应性。
Generic callback functions help developers write C programs that are versatile and better adaptable.
在此章节中,我们解释了你可以如何使用函数指针,以便我们增强 C 程序的灵活性。此外,我们还演示了你可以如何创建通用回调函数,它并非受特定函数指针类型限制。
In this chapter, we explained how you can use function pointers so that we can enhance the flexibility of our C programs. Additionally, we showed how you can create generic callback functions that are not limited to a specific function pointer type.