Piyush
Piyush

Reputation: 65

Clarification on concept of callbacks and function pointers in c

I found this line on wikipedia about function callbacks, "In computer programming, a callback is a reference to executable code, or a piece of executable code, that is passed as an argument to other code. This allows a lower-level software layer to call a subroutine (or function) defined in a higher-level layer.”

I did not get the line "This allows a lower-level software layer to call a subroutine (or function) defined in a higher-level layer.” I am wondering can we not call a function of a higher-level directly?

Upvotes: 1

Views: 227

Answers (1)

Rohan
Rohan

Reputation: 53316

One of the advantages is -

Callback - function pointer - makes function name variable. You can define function with any name and pass that to lower layer as function pointer.

Otherwise, each time higher level would have to define function with same name.

Also, this makes higher and lower layers very loosely coupled. If lower layer uses the function name directly, it needs to get resolved during compilation. If the lower layer is shared library, then linker will raise an error and will not be able to compile it.

Upvotes: 2

Related Questions