Callback Functions in C Explained

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi in this video i'll talk about callback functions also i will show how to implement them in the c programming language so let's get started what is a callback function well it's a function that is passed to another function as an argument this other function shall then execute the callback function at some point in time there are two types of callback functions depending on when they are executed if they are executed immediately or before this other function returns the callback function is synchronous if the callback function is executed later or after this other function returns the callback function is asynchronous callback functions are commonly used in event driven systems so for example when you click a button in some application or if you press some keys on your keyboard a certain event will happen this event has to be handled somehow this is where callback functions come in and they will handle this event but how do we implement callback functions in c we need a mechanism to pass the callback function to another function as an argument this can be easily done by using function pointers a function pointer can be used to point at a callback function and then executed at some point in time so without further ado let's get started with some examples in this first example let's define a calculate function that takes in two arguments an integer and an operation this operation is actually the callback function this calculate function should return the result of the operation on the integer let me quickly implement this the calculate function takes in an integer and an operation in the form of a function pointer if this is the first time that you've seen function pointers the syntax may be confusing for you but actually it's fairly simple in the middle we have the name of the function pointer and it points to a function that takes in an integer argument and returns a float value in the definition of the calculate function we just have the operation on the integer now let's define some callback functions that we can pass to the function pointer you may have already noticed that all this callback functions have the same signature as our function pointer they all take in an integer argument and return a float value just like here the next step is to call the calculate function and to pass in some callback functions so let's do that now here in the main function we just called the calculate function and passed in an integer number 5 and all of the callback functions the square the cube and the square root of our integer then we just print it to the terminal by using the printf function let's compile and run this program to see if it works okay we have a warning i guess we have to include standard input output library let's try to compile it again and let's run the exe file okay so the output is as we expect we have the square the cube and the square root of the number five we can simplify this function pointer syntax by using the typedef keyword in c so by writing this line we defined a data type operation type of a function pointer that points to a function that takes in an integer value and returns a float value [Music] down below we can just write the name of our new type and the name of our function pointer let's try to compile and run our program again and the result is the same as before and now let's jump in into another example let's take a look at the q sort function that is inside the c standard library it's interesting because we can actually pass in a callback function to this function pointer the q sort function also accepts some other arguments [Music] and basically what it does is it sorts the elements of an array uh based points to this array num is the size of the entire array and size is the size of the individual elements in the array the callback function that is passed into this function pointer determines the way in which the elements are sorted so let's take a closer look at how it works our callback function definitely has to have the following prototype it has to return an integer value and it has to accept two constant void pointers the callback function determines the order in which the elements of the array are sorted by returning different values so if the callback function returns a value less than zero then the p1 value goes before the p2 value if the return value is zero then these two elements are equivalent and if the return value is greater than zero then the p1 element goes after the p2 element so now let's actually try to use this q sort function inside an actual example i'll keep the q sort reference at the right side so you can take a look at it if you need to let's sort an integer array in the ascending order by using the q sort function let's start by implementing the callback function that will define this ascending order for the implementation of our callback function i just copied and pasted this function prototype from the reference [Music] then i defined two integers these two integers get their values from these void pointers and the way in which they get the values is that we have to cast these void pointers to integer pointers and d reference them to get the values here we have three comparisons and three return values so if the element i1 is smaller than the element i2 then the element i1 goes before the element i2 so we have to return -1 if the elements are equivalent we have to return 0 and if the element i1 is greater than the element i2 then the element i1 goes after the element i2 so we return 1. now let's sort an actual array by using the q sort function in the main function at the start of the main function we created an integer array of five elements then we printed this initial array after that we sort the array by using the q sort function we had to provide all the necessary arguments to the q sort function the most important argument is of course the callback function which defines the ascending order and at the end we just print the sorted array now let's see if the program works and as expected the initial array is now sorted okay so that's all i had for callback functions please drop a like subscribe if you want to see more and i'll see you later
Info
Channel: Codeflash
Views: 44,811
Rating: undefined out of 5
Keywords: callback functions, callback functions in c, callback functions explained, callback functions example, windows, vscode, gcc, coding, programming, computer science, tutorial, how to, codeflash
Id: Hm1OjzTa_MY
Channel Id: undefined
Length: 13min 25sec (805 seconds)
Published: Sun Aug 09 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.