Ramilol
Ramilol

Reputation: 3462

What is difference between callback function and regular function?

Ok I'll give two examples of function is using CALLBACK and regular function.
Note: I didn't write these examples.

Regular Function

int sumExample (int a, int b)
{
    return a + b;
}
int main()
{
     int = sumExample(1, 3);
     cout  >> int;
     return 0;
}

Function using _stdcall

int __stdcall sumExample (int a, int b);

what is the difference?
Note: I'm not sure how Calling Conventions works, an example would help!

Upvotes: 1

Views: 1668

Answers (3)

mrbean
mrbean

Reputation: 581

A callback is a function pointer (i.e. address) that is passed to another piece of code.

The address of the callback function is passed to a normal function.

This allows a lower-level software layer to call a function defined in a higher-level layer.

https://en.wikipedia.org/wiki/Callback_(computer_programming)

Upvotes: 0

Puppy
Puppy

Reputation: 146998

Basically, a calling convention specifies implementation details of how the function will be called. Most libraries use the Standard C calling convention - __cdecl. WinAPI however expects __stdcall. You only need to know two things about calling conventions: that they have to match, e.g., you can't convert a void(*)(int, int), which is implicitly a void(__cdecl *)(int, int), to a void(__stdcall *)(int, int), and that the default is __cdecl. CALLBACK is just a WinAPI #define so that they can change if they want to.

Upvotes: 2

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799230

Not much, really. A "callback" is a name given to a function to be passed to another function that will "call it back" when... something useful happens. There's no reason it can't also be used as a regular function though.

Upvotes: 2

Related Questions