C Programming

Callback Function in C

A callback in the C programming language is an event-driven function that passes a function into another function as an argument. Simply the callback function is when a reference of a “function 1” is passed as an argument to “function 2” using a function pointer. That function uses that reference to call “function 1”. This article will provide a few illustrations on the usage of a callback function in the C programming language

C:\Users\sabah\Documents\office\technical writing\callback.jpg

Syntax

For using callback function, we will have to declare and initialize function pointer in the main function. Function pointer declaration/initialization syntax is defined here:

[Returntype-of-the-callingfunction] (*[Pointer-name])([Callingfunction’s-parameters])=& [Callingfunction-name];

After declaration/initialization of the function pointer, we will pass the pointer to the desired function in the following appended way:

[Desired-Function-name]([Any-other-function-parameter], Pointer-name);

The function definition/initialization signature of the function receiving that argument will be like this:

[Returntype] [Function-name]([Any-other-function-parameter], [Returntype-of-calling-function] (*[pointer-name]) ([Calling-function’s-parameters])

At last, calling that function using that passed pointer is displayed here:

[Pointer-name] (that-function's-parameters);

The theory might be a bit confusing. However, implementation of the examples will help you clear those confusions.

Example 1:

In our first example, we will create a simple callback function. That returns nothing and takes no parameters. To clear our newly learned concepts by implementing them. All you need to do is create a notepad and give it a title of your choice. Add .cpp extension used for C programming files.

Once the document is created, open the command-line interface and type the following appended query to compile your C language code using GCC compiler.

$ gcc -o [your filename] [your filename].cpp

Now, type to the subsequent command for the execution of code.

$ [Your filename].exe

Let’s jump to the main code. We have started by creating function signatures at the top, as we need to create two functions. One will be the callback and the other whose function pointer is being passed to the callback function.

In the main function, we will declare/initialize our function pointer. While function call, we need to pass the pointer as an argument. The main function code is presented in the screenshot underneath.

Now, all we need to do is fill in our callback and the other function. Identical to the way in the image presented below. The callback and other function codes are presented in the underneath screenshot.

Our functions are not performing anything complex. For getting the basic idea, we will print messages on the console. To see how the callback function is working. Execute the instruction that is presented in the underneath image.

Judging from the output, we can tell when we did function calling from the main. It went to “function 1” and printing the message on the console. Then using the function pointer, “function 2” is accessed when the lines of code in “function 2” are all executed. The control will go back to “function 1”.

Example 2:

In this case, we will implement the calculator’s simple operations (i.e., add, subtract, multiply, and divide) with the help of the C language’s callback function. We will start by adding function signatures of operations and the callback function similar to example 1.

Then we will declare our integer, operation, and the function pointer variable.

Take user input for integers to operate on, and ask the user to choose their desired operation to perform on them.

Using the else-if condition, we will make a user-chosen operation function pointer to pass it into the calculator callback function.

That’s all for the main function, time to code callback, and operation functions. All the operation functions signatures take two arguments and return the operation result as an output. In our calculator function, we will print the returned value of the operation by calling the operation function using its pointer.

For all the operation functions, we will code to compute and return the result of that operation.

At last, it is time to test our code. Execute the instruction that is presented in the underneath image.

As you can see, the program is working smoothly without any errors. Let’s try to test other operations as well.

As you can see, our program is running logically correct for all the operations. When a user selects their desired operation, that particular “if” of their operation choice is selected, and that operations function pointer is passed to the calculator function. Using that pointer calculator function calls the executable code of the chosen operation and as, a result, gets back the resultant answer.

Now we will test how well our program reacts when a user enters an invalid input for choosing an operation.

As you can view from the attached image presented above, that our program is working smoothly.

Conclusion:

Let’s do a quick overview we covered in this tutorial, is the theory of callback function, function pointer, its syntax, and implemented few examples to get a better understanding. I hope this tutorial helped you fix your concepts and clear all your queries regarding Callback functions in the C language.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content