Hàm Callback, callback, call back

46 views
Skip to first unread message

Nguyễn Thành Vinh

unread,
Nov 25, 2012, 8:23:53 AM11/25/12
to picac...@gmail.com, k15...@googlegroups.com
Hàm callback nghỉa là một hàm được sử dụng như là một tham số đầu vào của một hàm khác
Ví dụ trên C:

#include <stdio.h>
#include <stdlib.h>
 
/* The calling function takes a single callback as a parameter. */
void PrintTwoNumbers(int (*numberSource)(void)) {
    printf("%d and %d\n", numberSource(), numberSource());
}
 
/* A possible callback */
int overNineThousand(void) {
    return (rand() % 1000) + 9001;
}
 
/* Another possible callback. */
int meaningOfLife(void) {
    return 42;
}
 
/* Here we call PrintTwoNumbers() with three different callbacks. */
int main(void) {
    PrintTwoNumbers(&rand);
    PrintTwoNumbers(&overNineThousand);
    PrintTwoNumbers(&meaningOfLife);
    return 0;
}

Kết quả:

125185 and 89187225
 9084 and 9441
 42 and 42

picachucode
picachubook
picachuc

--
Thunderbird
Name: Nguyễn Thành Vinh
Address: Đà Nẵng, Việt Nam (vietnamese)
Company: K15TPM Đại học Duy Tân
Yahoo: picachusays

Email: picac...@gmail.com
Reply all
Reply to author
Forward
0 new messages