简体   繁体   English

为什么回调函数的名称回调?

[英]why is the name callback for the callback function?

please don't care or go strict on correcting the syntax. 请忽略或严格纠正语法。 The point more important is why is the name callback for the callback function? 更重要的一点是为什么回调函数的名称回调?

for example:- 例如:-

function drive(int x, int y);
function breaker(int x, int y, callbackFunction);
int function main(){
    drive(23,24); // why is this called a "call to a function"?
    breaker(23,45,drive); // why "drive" here is called a callback and why not "call to function" like other function.
}

function drive(int x, int y){
   printf("%d%d",x,y);
}

function breaker(int x, int y, callbackFunction){
  callbackFunction(x,y);
}

The point here is not the accuracy of syntax. 这里的重点不是语法的准确性。 The point is i am calling the "drive" in main method and that is called as "calling the function" and when i pass the "drive" function as an argument to other function in "breaker" the "drive" is called as callback but why? 关键是我在main方法中调用“驱动器”,即所谓的“调用函数”,当我将“ drive”函数作为“ breaker”中其他函数的参数传递时,“ drive”被称为回调但为什么? any call to function have to refer to function and should execute it but why the difference in names? 任何对函数的调用都必须引用函数并应执行该函数,但是为什么名称有所不同?

breaker(23,45,drive); // why "drive" here is called a callback and why not "call to function" like other function.

This does not call drive() . 这不会调用drive() This is a call to breaker with the address of the function drive as a parameter. 这是使用功能drive的地址作为参数的breaker的调用。 The abscence of parenthesis denotes that we are not calling drive, we are passing the address of the function as a parameter. 括号的不存在表示我们不调用drive,而是将函数的地址作为参数传递。

The purpose of doing so is so that breaker() can call drive() as many times at it needs. 这样做的目的是使breaker()可以根据需要多次调用drive()

Edit: 编辑:

The name callback is because the functions you are calling is calling your function back. 名称回调是因为您正在调用的函数正在回调您的函数。 This is more evident when the function with a callback parameter is provided by someone else. 当带有回调参数的函数由其他人提供时,这一点更加明显。

Take the qsort function for instance. qsort函数为例。 qsort is a standard C function that takes a comparator callback function and an array to sort. qsort是一个标准的C函数,它带有一个比较器回调函数和一个要排序的数组。 You call qsort() and qsort() calls your comparator whenever it needs to compare two elements in the array. 只要需要比较数组中的两个元素,就可以调用qsort()qsort()调用比较器。 So a standard C function calls back to your code. 因此,标准的C函数会回调您的代码。

breaker(23,45,drive); 

this will replace callbackFunction with drive 这将用驱动器替换callbackFunction

function breaker(int x, int y, drive){
   drive(x,y);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM