C Programs Tutorials | IT Developer
IT Developer

C Programming - C Callback Function



Share with a Friend

C Programming - C Callback Function

C Callback Functions

A callback function in C is a function that is passed as an argument to another function. The callback function is then invoked (called back) inside the other function, typically at some point during the execution of that function. This allows a level of customization and flexibility in how a program operates, particularly when you want a function to perform an operation, but the specific details of the operation should be provided by the caller.

What is a Callback Function?

  • A callback function is a user-defined function that is passed as an argument to another function.
  • The function receiving the callback function will execute it when appropriate, providing a way for the caller to dictate behavior or modify the flow of the program.

How Callback Functions Work in C

  1. Define a callback function: The function you will pass as an argument.
  2. Pass the callback function: The callback function is passed to another function that will invoke it.
  3. Invoke the callback: The receiving function calls the callback function at the right point in its execution.

Callback Function Example in C

Here is a simple example of a callback function in C:

C

#include <stdio.h>

 

// Callback function definition

void printMessage() {

    printf("Hello from the callback function!\n");

}

 

// Function that accepts a callback function as an argument

void executeCallback(void (*callback)()) {

    printf("Before calling the callback function.\n");

    callback();  // Calling the callback function

    printf("After calling the callback function.\n");

}

 

int main() {

    // Passing the callback function to executeCallback

    executeCallback(printMessage);

    return 0;

}

Explanation:

  • printMessage is the callback function that simply prints a message.
  • executeCallback is a function that accepts a pointer to a function (void (*callback)()) as a parameter. It calls the callback function when needed.
  • In main(), we pass the printMessage function to executeCallback, which then calls printMessage inside its body.

Output:

Before calling the callback function.

Hello from the callback function!

After calling the callback function.

Callback Functions with Parameters and Return Values

Callback functions can also accept parameters and return values. Here’s an example of a callback function with parameters and a return value:

C

#include <stdio.h>

 

// Callback function definition with parameters

int add(int a, int b) {

    return a + b;

}

 

// Function that accepts a callback function with parameters

void executeOperation(int x, int y, int (*operation)(int, int)) {

    int result = operation(x, y);  // Calling the callback function with arguments

    printf("Result of operation: %d\n", result);

}

 

int main() {

    int num1 = 5, num2 = 3;

    // Passing the add function as the callback function

    executeOperation(num1, num2, add);

    return 0;

}

Explanation:

  • add is the callback function that adds two integers.
  • executeOperation takes two integers and a function pointer (operation) that points to a function that takes two integers and returns an integer.
  • Inside executeOperation, the callback function (add) is invoked with the arguments num1 and num2.

Output:

Result of operation: 8

Common Use Cases for Callback Functions

  1. Event Handling: Callbacks are often used in graphical user interface (GUI) programming where actions like button clicks or mouse movements need to trigger specific functions.
  2. Sorting Algorithms: Callback functions are used in sorting routines to compare elements.
  3. Asynchronous Programming: Callback functions are commonly used in asynchronous programming models to handle events or responses when they are received.
  4. Custom Behavior: Callback functions allow a program to execute user-defined code at specific points without modifying the core logic of the program.

Advantages of Using Callback Functions

  1. Separation of Concerns: The main function can delegate behavior to the callback, allowing different behaviors without changing the core logic.
  2. Flexibility: Callbacks provide flexibility to extend or modify behavior at runtime.
  3. Reusability: You can reuse a function and provide different behaviors by passing different callback functions.
  4. Modularity: Callback functions allow for clean separation between the logic of one function and the behavior passed in through the callback.

Conclusion

  • A callback function is a mechanism where a function can accept another function as an argument and call it at an appropriate time during its execution.
  • Callback functions make C programs more flexible and modular, allowing developers to customize behavior without changing the main function's logic.