This article is written on the assumption that you know the concept of the Pointer
When we use API, we can study about the Callback function. And we can know why people usually use the Funciont Pointer (in API operation).
A Pointer has a address of its variable. Likewise, a Funtion Pointer has a address of its function. This Function Pointer can be used as a parameter of other function. And it is applied on the Callback Function. (So we have to know what is teh Function Pointer)
Data type of Function Pointer must match with data type of function referenced by.
Definition of a basic function to refer to the address
int sum(int a, int b){
return a+b;
}
Declaring the Function Pointer
Match the data type (function”sum
” , factor)
int (*fp)(int, int); // 반환 자료형(*Name) (factor type, factor type)
Using Function Pointer
fp = sum;
Code (Total)
#include <iostream>
using namespace std;
int sum(int a, int b){
return a+b;
}
int main(){
int (*fp)(int, int); // 반환 자료형(*Name) (factor type, factor type)
fp = sum;
cout << fp(1,2) << endl;
return 0;
}
*3*
In normal case, functions are called out by an user when he or she want to operate. But sometimes, some interrupts occur in time we can’t predict, then some functions are called out. We call these functions to “Callback Function(s)” For example, we can catch the call when we are playing the game. Then the screen of our phone will be changed to express the Call Screen. At this moment, the call(exactly, the function expressing the Call Screen) is the Callback Function.
We can often find the Callback Function in the case of calling out between the Applications and the OS. We make the code of Callback Function in the Application, and make the code of calling out in the OS. And the Callback Function will be called out if an specific event is occured in the OS.
Callback Functions are materializated by using a Function Pointer
void UserFunction(){
cout << "A" << endl;
}
void SystemFunction(void (*fp)()){
if(event) fp();
}
int main(){
SystemFunction(UserFunction);
return 0;
}