Callback function คือโค้ดที่ executable ถูกส่งเป็น argument ให้โค้ดอื่น เพื่อเรียกกลับ (call back) หรือ execute ในเวลาที่กำหนด
ใน C ใช้ function pointer เพื่อเรียก callback function
ตัวอย่างโค้ด
#include <stdio.h>
void my_function() {
printf("This is a normal function.");
}
void my_callback_function(void (*ptr)()) {
printf("This is callback function.\n");
(*ptr)(); // calling the callback function
}
int main() {
void (*ptr)() = &my_function;
my_callback_function(ptr);
return 0;
}ผลลัพธ์
This is callback function. This is a normal function.