Callback function instance

/*
*A callback function is a function called through a function pointer.
* If you pass a pointer (address) of a function as a parameter to another function,
*When this pointer is used to call the function it points to, we say this is a callback function.
*/

#include <stdio.h>


//function pointer definition
typedef void (*pcb)(int x, int y);


/*
*Callback function GetCallBackA
*/

void sum(int x, int y)
{
	int temp;
	temp = x + y;
	printf("x + y = %d \r\n", temp);
}

/*
* Implement the calling function GetCallBackA of the callback function
*/
void GetCallBackA(pcb callfun, int a, int b)
{
	printf("GetCallBackA Test:\r\n");
	callfun(a, b);
}

/*
* Implement the call function of the callback function Callback function GetCallBackA
*/
void GetCallBackB(void(* callfun)(), int a, int b)
{
	printf("GetCallBackB Test:\r\n");
	callfun(a, b);
}

int main(int argc, char *argv[])
{
	GetCallBackA(sum, 4, 6);	
	GetCallBackB(sum, 4, 6);
	printf("Hello C-Free!\n");
	return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325985189&siteId=291194637