Callback function versus regular function in C

I learned about the callback function in C and found that it is too difficult to understand the concept of a callback.

As I know, a callback function is implemented using a function pointer in c, which means that we can refer to a function using a pointer in the same way as we used a pointer to refer to a variable.

I have two functions:

1. First, the callback function is used.

#include <stdio.h>

int add_two_number(int a, int b);
int call_func(int (*ptr_func)(int, int), int a, int b);

int main (int *argc, char *argv[])
{
     printf("%d", call_func(add_two_number, 5, 9));

     return 0;
}

int add_two_number(int a, int b)
{
     return a + b;
}

int call_func(int (*ptr_func)(int, int), int a, int b)
{
    return ptr_func(a, b);
}

2. The second uses a regular function call:

#include <stdio.h>

int add_two_number(int a, int b);
int call_two_number(int a, int b);

int main (int *argc, char *argv[])
{
    printf("%d", call_two_number(5, 9));

    return 0;
}

int add_two_number(int a, int b)
{
    return a + b;
}

int call_two_number(int a, int b)
{
    return add_two_number(a, b);
}

These two functions make a simple mathematical addition between the two numbers and the two functions also work correctly, as I expected.

My question is what is the difference between the two? and when do we use callback instead of normal function?

+4
source share
6

, , , . , :

#include <stdio.h>

int add_two_number(int a, int b);
int sub_two_number(int a, int b);
int call_func(int (*ptr_func)(int, int), int a, int b);

int main (int *argc, char *argv[])
{
     // Here is where we decide what the function should do for the first call
     printf("%d", call_func(add_two_number, 5, 9)); 
     // Here is where we decide what the function should do for the second call
     printf("%d", call_func(sub_two_number, 5, 9));
     return 0;
}

int add_two_number(int a, int b)
{
     return a + b;
}

int sub_two_number(int a, int b)
{
     return a - b;
}

int call_func(int (*ptr_func)(int, int), int a, int b)
{
    return ptr_func(a, b); // Here is the place the call is made
}

. . , , , . , , .

+3

Inversion of Control. , , , , , " ". , , :)

, .

+1

, , , . ""

function do_something(pointer foo) {
}

, "do_something" LOT , "" .

do_something(&callback_func_number_1);
do_something(&callback_func_number_2);

...

, , , , .

, do_something :

do_something_and_call_data_from_func1() { ... }
do_something_and_call_data_from_func2() { ... }
+1

?

, , ​​.. , . "", call_func, , .

:

#include <stdio.h>

int add_two_number(int a, int b);
int sub_two_number(int a, int b);
int call_func(int (*ptr_func)(int, int), int a, int b);

int main (int *argc, char *argv[])
{
     if (argv[1][0] == '+')
         printf("%d\n", call_func(add_two_number, 5, 9));
     else if (argv[1][1] == '-')
         printf("%d\n", call_func(sub_two_number, 5, 9));

     return 0;
}

int add_two_number(int a, int b)
{
     return a + b;
}

int sub_two_number(int a, int b)
{
     return a - b;
}

int call_func(int (*ptr_func)(int, int), int a, int b)
{
    return ptr_func(a, b);
}

, ( if).

?

, . , 4 / , , , . qsort . , GLFW, ( ) : . .

0

, . , , .

( , ) , , . , , . . . , vnode, . switch if-then-else. " " , , ( ).

0

. , .

? .

?
, Event Driven (, ) . (), ( ) , - . " " , , .

Callbacks are useful if they are pointers, because you can conveniently change them (to a different set of functions), but you don't have to use them that way. If your main program was a kind of switch that responded to an event, then you can refer to any of your versions as a "callback" function.

0
source

Source: https://habr.com/ru/post/1589490/


All Articles