C_102 Callback Function using Function Pointer in C | C Language Tutorials

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so in the series of learning c programming we are discussing functions in c in the last video we have discussed what is function pointer the basics what is function pointer how to use a function pointer how to declare it how to initialize it and we have just seen a simple program right basics about function pointer we have discussed in that video now in this video we'll discuss one application of function pointer why we use function pointer and that is nothing but callback function function pointers are used what in callback function what is this right how to use it how to know call back a function that we will discuss here with a simple program right and i will show you practically so we will run that code and i will show you what output you are getting what is callback function right so everything about qualifier function we are going to discuss in this video but before they just want to tell you one thing if you have a prank for gate or will be preparing for gate 2023 exam then an academy is going to start a new batch from 29th of september for gate and escs parents it's you know rohan for gate and esc in this course the top educator will cover all the subjects of cs and iit and helps you to prepare in your gate exam here you will get the detailed explanation of all the concepts and apart from this some special classes would also be there like they'll discuss preparation strategies some tips and tricks shortcuts to solve some questions practice questions and numericals so that these classes will really be helpful for you guys and the course will be covered in both hindi and english and notes should be given to you in english only plus some doubt solving session would also be there so you can you know get your all your doubt cleared in those doubt solving sessions and this is paid subscription because you will get a complete and structured course but here the special price week is going on so if you go for a subscription 12 months or above then you will get it at a special price with 10 discounted rate you can see the detail of the price on the screen the regular price after 10 percent off and if you will use my code jkl 10 then you will get extra 10 off on that reduced price so the final price you can see over here so if you are interested you can go for the subscription you can find out the detail of this course in the link that i'll put in the description box of this video you can go and check out so now let's see what is call back function this is you can say one type of application or function pointer i guess you know what is function pointer you must watch out my previous video first then come to this video then you will get it better function pointer is what it's like a normal pointer variable but it contains address of a function so function pointer contains address of the code and normal pointer contains address of the data right or the value right and now function pointers are used to call back a function now what does this mean see let's take a simple example here i am taking suppose two function one is sum here i am doing sum of two numbers one is subtraction here i am doing subtraction of two numbers right and from another function suppose a function is display from this function i want to call the sum and subtract right means callback function means nothing but we are passing function to a function if we pass here something like uh if you write here in a into b it means what we are passing arguments to this function right but here we can pass a function also as an argument in these arguments we can pass a function means function to a function that is callback function we are passing address of another function to a function that is what callback function now see here we are taking these two function and this display function is going to call this and this but at runtime whatever here you will pass here we are going to pass address off if you will pass address of this function then it will call sum function when you call when you pass address of this function here then it will call this function same if we have suppose 10 functions so from one function we can call all the ten functions one by one one time you can pass address of this function and under another time when we call this function just pass address of this function second function third time when you call this just pass address of the third function so one function only can call all these functions it depends what address you are passing at runtime only so this display display function doesn't know to which function he is going to call but whatever address you will pass here that function it is going to call it runtime this will discuss with proper program also right now see see what what is happening here suppose here i am taking this example uh i'm taking void sum and here i'm taking into a comma into b and here i'm just doing printf percentage d a plus b right and this function is i am taking two argument in a into b and simple here print f percentage d a minus b right two function we have right now one function we have what this display so in display as an argument we are going to pass address of either this or this and if you want to pass address of these function then you must have a pointer to accept that address right so if you are passing address of a function then here we must have a function pointer to accept address of that function obviously so here you have to declare a function point right now how to declare that see both the function are having same data type that is void just function pointer name i'm taking fptr and you have to put it in brackets and here both the function are taking two arguments so just write down intent i hope you know how to declare a function pointer that we have discussed in previous video so here we are taking a function pointer as an argument to this function now from this function we are going to call these functions right so how to call using this function pointer because this function pointer is having address of these functions right now how to call f ptr and just pass the argument like i am passing 5 comma 1 that's it or simply you can write fptr rather than this s trig that is also fine that will work right 5 and 1. now in main function what we are doing in main function suppose i am calling what here i am calling this display function display and this time i am passing address of this function so how to pass address simply you can write address of sum or rather than ampersand you can simply pass the name of the function that also means you are passing address of this base address of this function right next time when i will call i say display and subtract that's it means we are passing a function to a function so for this display function sum and sub these are callback function right we are calling this function display and this function is calling back or calling again these functions means the the sum and sub these are callback function for this display function callback function right we are calling again these function from this function right so see control will go here now we are calling this so display sum we are passing address of the sum obviously this and the sum will having will have some address in the memory starting address we are passing or if you want to find out the address you can simply print the address also right like printf address of the function is percentage p and comma name of the function then you will come to know that this these functions are also having address so it will print the base address of this function if you want to print you can print right when we run this code i will show you that thing also right now address of this function is suppose in the memory this function is here these are having instructions of this function sum and suppose memory is the base address is hundred thousand and suppose in memory this function is sub having instruction one two three of sub and the starting address is suppose one zero four six so here we are passing thousand so to accept this now control will go here to accept this address there must have a function pointer so yeah we have a function pointer right void strict fptr and and right so see now these the return type and the number of arguments these should be same of these function right because we are taking only one pointer one function pointer to call these functions suppose here the data type is float and here we are taking float then obviously this function pointer will contain address of a function which return type is void and which will accept two argument in in and in so it can have address of this function only if you assign address of this maybe it will show some unusual behavior because return type of this is float so i hope you are getting my point all these functions should be of same type like void and here also we are having in because only one function pointer we have using that function pointer only we are going to access all the functions maybe you have two three or ten functions right so now here we are passing thousand so in fptr we have thousand so now this fptr is now pointing to suppose we have a in memory we have fptr this one and now this is pointing to here thousand so within this first of all we are going to dereference it strict ptr the value at this ptr is this code and now in this code we are passing five and one so here it will go to here now here we are passing five and one so five and one so it will print six right next time we are calling sum so this time it will pass 1046 so now at this time fptr is having one zero four six so now it is going to call this function now in we have five and one subtraction would be four so it will print six and four so see using one function only we can call these two functions right at runtime you can pass the name of the function or the address of the function and you can call that function simple as that using function pointer and this is possible only using function pointer so this mechanism is nothing but call by method right and here call by a function callback function is what sum and sub right so i hope you got this thing see i have taken a simple example because this is a little bit confusing callback function this kind of thing so that's why i have taken a simple example so now let's run this and see what output you are getting you can try out different different programs that are of complicated level at your own using function pointer right so now let's create a new file callback function dot c right and here you will take let's take two function void sum and here we are just printing sum equal to percentage d slash n and a plus b you can also write down like return a plus b that is also fine but rather than word there then you have to use int right that is also fine you can take this thing now here we are having next function is sub and just i am printing a minus b right and one more function is what i am taking display so here we'll take function pointer f ptr and it should accept two arguments and here from here we are calling so f b d r and i am passing the argument suppose 5 comma 1 now in main what we do we will call display and first time we'll pass the address of sum and next time we'll pass sub right so now let's run this and see what output you are getting see sum is equal to 6 and sub is equal to 4 so i hope you go to this now you can try out different different variations and different different types of programs you know passing a function to a function as an argument that is nothing but a callback method so that's it for this video now so now in the next video we'll see even more application of function pointers and i'll see in the next video till then bye bye take care
Info
Channel: Jenny's lectures CS/IT NET&JRF
Views: 5,531
Rating: 4.8415842 out of 5
Keywords: data structure tutorials, operating system, data structure and algorithms, jayanti khatri lamba, jennys lectures, jenny data structures, jennys lectures DS, jenny lamba, jennys baby, jennys lectures baby, data structures, what is callback function in c, function pointer in c, c language tutorials for beginners in c, what is function pointer in c, what is callback method in c, file hndling in c, structure and union in c, gate cs lectures, previous year gate question answer
Id: wMI2k951nqs
Channel Id: undefined
Length: 14min 30sec (870 seconds)
Published: Tue Sep 28 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.