Hello everyone, my name is (name) and we are continuing with this C programming course.
In the last videos, we had discussed the pointers, we had seen how we can exactly point a variable with the help of a pointer.
We have seen how a variable starts at a particular location, its value, how it stores the value at the address and that address whichever variable stores in itself, we call that as the pointer and in this way one pointer points the other variable, points the value of the other variable.
So, we saw how we can point the other variable with the help of a pointer, but in this video, we will see how we can point our function with the help of a pointer.
It means, how? the function which has a return type in which we are passing the argument and on the basis of an argument we have written few statements in our function.
So, if I talk about my pointer, what was my pointer doing till now, it was pointing at the address of some variable but if I want that entire function in which there are different statements, there are different members, if I want to point my pointer on that address, how can that be done? We will be seeing that in this video. okay?
This is extremely simple, because we know that how we were pointing the variable, we used to write the return type and in front of it, we used to write the variable’s name.
That’s when the pointer used to point to it. right?
And now here the flow will be exactly of that type, it will be similar to what it was earlier with the variable.
Here we'll have to do a little bit of minor changes.
Why? Because we are pointing to a function.
And the function’s structure and flow basically are different from a variable.
So, how we will point the function over here, that we are going to see here.
So, we will quickly start with our topic, with our video and we will see how to exactly point to a function with the help of a pointer. alright?
So, if I see here, how we can point a function here.
We have seen that it is written here, “just like the way we point to an integer value with a normal pointer, we can also point to a function in a similar fashion”.
The way to write is extremely similar.
I will not call it exactly the same because the entire flow becomes different for one variable and for one function but the way of writing is extremely similar, so we will not have a lot of trouble understanding it.
Now how to understand it.
If you see it theoretically, you might find it a little bit confusing.
But if I say that we have to understand it with the help of one practical, how the pointer is pointing to one function then we'll be able to understand it easily.
And now, we will quickly go to our practical screen which means inside our text editor and there we will see one such program in which we are using a pointer which points at a function.
Now, if we talk about why we have to point at this function, we were pointing to a variable, we were manipulating its value.
What is the need that we have to also point at the function? All the answers to these questions will be seen ahead in the video.
But before that, let's understand that how the function should be pointed which means the address of the function, basically on which storage location the function has been stored inside of a computer, how can we store that address inside of a pointer so that any time, whatever things has been on that address, we want those, so will be able to access to easily access it, to do all these things, we will go to our text editor.
We have come to our Visual Studio Code screen and we are talking about a very simple program where what we are doing is we have made one function and along with that we have also made one pointer.
Now, what we will do is, this pointer we will point towards our function and after that what we will do is this extremely exciting part, we have pointed towards the function, we have stored the address in the pointer of our function, but what next? So, what we will do in a program is like we used to call our function, like we used to pass the arguments and write the function’s name and then values used to be fetched by the function definition and over that the processes and the calculations or the operations used to get performed.
What we will do here is, now since we have pointed function towards the pointer.
Even through the pointer’s name we can call this function and pass the arguments, so that the function definition should go in that fashion or flow.
There is no problem that is created over here.
So, here this is the benefit of our pointer of pointing towards a function.
We will quickly go to the program.
You can see in the program we have started with one preprocessor command which is very important and necessary, which is the core of our programme.
So, that's why we have included the S T D IO dot H file over here.
Because we have to use the input and output operation function over here.
Then we spoke about the pre-processing command, after that, if you see we have not started the main function over here on the contrary, we have written a function definition, over here before the main function we have written the function definition.
So, there is no need to tell our compiler from before, what will be our function, what will be the data types in it what will be the arguments and there is no need for us to tell on these things, there is no need to declare the function as we have written the function definition early on.
So, after writing the function definition our compiler gets to know what exactly we are trying to do here.
So, if you talk about function definition, here we have named the function F U N fun and the return type of the function is void which means it is not returning anything, it will not return anything whichever function we will call, it will not return the value over here.
Inside this there is a simple printf statement, which will print one value, but there will not be anything that will be returned.
You can see that here we have also made one argument which is integer type and here we have given it a name A.
What we are doing in it is, we have written one printf statement and inside that we have displayed, value of A is, after that we have used the format specifier and after that whatever will be the A’s value at that time of execution, after value of A is it will get printed.
So, this is a simple function, we have written, simple function definition.
So, till here all these things are clear to us, we have also understood how we have defined the function here.
Now, we will go ahead and come to our main function.
As soon as the main function starts, you can see that we have written a few things over here, these are basically the pointer which is pointing at the function which means it is storing the functions address in itself.
So, how we will write it, we will quickly see that over here.
The way to write the pointer, we know very well, before any variable’s name we have to put one asterisk mark, due to which it basically becomes a pointer.
And this pointer’s return type should also match with that thing which basically we are pointing towards or whose address we want to store in the pointer.
Here, since we want to point the fun function, its return type or the void return type, that we have written here, V O I D.
So, this is the return type of the pointer.
We will go ahead of this and you will see that we have put one Asterix mark and we have written our pointers name, which is fun PTR.
Why is it one fun PTR because here we are pointing at fun, we are pointing at fun functions.
That's why we have named it fun PTR, you can give any name you want, there won’t be any problem.
So, this is our pointer’s name, which is called F U N PTR, which means fun PTR.
And we have represented it with the Asterix mark with which we can understand that this basically is a pointer.
Ahead of it, you can see that in the bracket we have written one data type, which is int integer.
Why have we written it over here? We have written it over here because whichever type of function we are pointing at, its address we would be storing, we have to write a few things here to match it.
Like if we have to talk about F U N functions.
Its argument was only one and its data type is integer over here.
So, even here, when we are making one pointer, in front of it, we have to put one round bracket and inside that whichever function we will point, its arguments data type has to be written over here.
Because of which we get to know that it can point that function and it will point that function.
Now, if we see here, there was only one argument in our fund function, whose data type was int.
So, even here we have written a data type int.
If there was one more argument over here, suppose in this way int B.
So, what we had to do here, we would have to put one more int, with which we can understand that this fun PTR will point only two argument functions.
Which is this F U N function.
So, here there is no need of B, we are practising a simple program.
So, we will write here only one data type of argument.
Now, you will see like we used to make the normal pointer, at that time near the pointer’s name, we used to not use the round brackets.
Why have we put the brackets here? If you talk about mathematics, in mathematics, there was one rule to solve any equation or expression which we used to call as BODMAS and what BODMAS used to do? It used to give some precedence order to the operators.
Whenever such kind of an expression comes in, first of all the bracket values must be solved.
After that, the division should be solved.
After that multiplication and in this way addition and subtraction.
So, there was some preceding order in the mathematics operation in this way even here the precedence is followed for different operators.
So, if you don't use this bracket here and we write it like a normal pointer, then what will happen? The round brackets which are over here, what will they understand, this fun PTR is my part.
And this fun PTR will start getting represented in this way that this fun PTR is also one function.
And this int is its argument.
But we don't want it that way.
We want that the Asterix should first get attached to the name after that this int argument round bracket should be considered.
So, to counter that case, we will put the round brackets over here because Asterix has more precedence than our brackets.
That's why the compiler considers the brackets first.
So, that scenario shouldn't be created here.
That's why we will put it in the round brackets because of which our pointer will be safe.
And Asterix is always stuck with our pointer’s name.
So, this was about writing ways of the pointer and after that we will access the address of the F U N function by writing an ampersand and we will assign that inside our pointer.
So, in this one line what we are doing, we are making our pointer and we are also storing our functions address in it.
So, this was about making the pointer and storing the function’s address in the pointer.
Now, we'll come ahead and as soon as we come ahead, what we have done here, we have called the function but with the help of pointer, which we are talking about.
Like we used to call the function and write its name and then in the brackets we used to pass the argument and our function used to easily work.
But in this case, what is happening here is the F U N PTR, the pointer which is there, it is helping us make the function call.
So, what we will do is whenever we want to make any such function call on which if any pointer is already pointing.
So, we will write the name of that pointer over here and we will write it in the round brackets, with the asterisk mark as well.
And after that, we will put these round brackets and whatever arguments we wanted here like we have only one argument over here, which is of an integer type.
So, we will pass the integer type value over here and we will use the semicolon.
Now this entire function call has got complete and here we have not mentioned anywhere our function name, right? We have not mentioned the function’s name but we have mentioned the pointer’s name and what is the pointer doing? It is pointing at the function.
So, whatever is the value inside it, at that particular address the compiler will go and then it will pass the 10 value over there and then our function will analyse it and it will perform the operations that it wanted to and here our output will come out.
So, what we have done here is here we have passed 10 as an argument for our pointer but it has eventually gone into our function.
Why? because our pointer was already pointing at the function So, 10 has gone here with the help of function PTR.
Here after accessing the printf statement, it will get executed and here whatever value of A that we had passed here, it will get printed here.
Now, we will see how this works? For that, we will save it and we will come to our terminal.
We will clear this now and we will compile our file, which is G C C.
And next, we will write our file’s name, the file’s name is “pointer to function” and we will compile it.
Now it has been compiled and now we will run it. -16:17
Now, you'll see that here a printf statement has been printed which is printing value of A is 10.
So, how did the a’s value come here? We have not listed it anywhere in the function that 10 should come here and apart from that directly even in the main function we had defined the value as 10.
But while invoking the pointer, the function call that we did with the help of the pointer, at that time the argument which was there, in that we had passed the value, 10.
And since we passed this value 10, because of that, in our function 10 has come and here in front value of A is 10 got printed.
So, you can see that the statement has been printed.
So, this was extremely easy.
I hope you understood the concept.
We saw how a function points at the pointer, in the same way it does to a variable.
What is the advantage that we are getting here? As soon as we used to call the function with the help of the pointers, we can call that function.
We just have to do one thing; we have to write the pointer’s name with the asterisk symbol.
And we will provide the arguments list in front of it in the round bracket and whatever execution that we have to perform with the help of that function, that can be followed completely over here.
This was all about the pointer to function.
Now, we will go on to our presentation and go ahead.
We talked about how the pointer should be pointed at the function.
Now about its implementation, where it can be used.
That we will see going ahead.
So, we will see here that the functions which are there, inside it there are the arguments list in that as well if we want to write any pointers, how can that be written? That we will be discussing here.
Now, what is the use of it? We have to put pointers even in the arguments list of our functions.
So, what is the exact need for this? So, if I talk about the two functions that we have, what do these two functions do? To both functions, suppose if I want to do such a thing that the first function’s all the values I have to get in my other function.
And we have to perform operations over it, can I do it directly? In C programming you cannot consider a function parameter and argument in another function.
But if you have to do this thing, in your scenario you have to perform such an operation in that situation to consider that situation.
We are getting a pointer as an argument concept, we know that our pointer is already pointing at some function which we have seen and learned.
We have to pass the same pointer as an argument to some other function.
So, what will happen over there whatever arguments that we have passed in the form of a pointer, that will be accessed by our function and from there the first function which was there, we would be accessing and manipulating the values of it. So, this was about pointer as an argument, what is the need here? So, “we cannot pass the function as an argument to another function”.
That's why if we cannot put another function in the argument of one function for that, “we can pass a reference of a function as a parameter by using a function pointer”.
Now, we have learned how to make the pointer, how we point the function.
We put that pointer as an argument over here for some other function and we will be seeing its usability over here.
So, this is one simple concept, how a pointer to a function we have to consider and we have to make it an argument in some other function.
we will understand this practically. we have seen in its syntax how one pointer is pointed towards a function.
Now we will know practically, how we can list down arguments in the pointer.
We will go again to our text editor.
And here we will open the pointer as an argument’s one program, where we have made two functions and what are these two functions doing? Over the first function, one pointer will point to it, that means one pointer will store its address in the pointer and the same pointer in some other function as a parameter, as an argument it will pass it.
So, here we are going to talk about it.
If we talk about our program, first of all we will start with our preprocessor command, where we are including the S T D I O dot H file over here, after that we have a display named function, whose function definition we have written and if you see the return type of it, that is void.
So, basically it would not be returning anything at the time of function call, it will not return any value which we would want to store in any variable.
That situation will not be created here.
So, its void data type has been made.
If you talk about the function, we have named the function display and, in the argument, we have written few pointer-like things, which we will see later, for now you just understand that as an argument we have to pass one pointer here.
That's why we have written one pointer over there.
So, this was about our display function.
In that you can see that we have written for loop.
So, what is this for loop doing? It is starting from one, which means I’s value is 1.
It is going on till 5.
Till the time the I’s value is not 5, for loop will go on.
You can see here this P and the brackets in it and I over here, this is one kind of a function call.
How does this function work? This we will consider here, since we have included one pointer here in the argument, the same pointer we will be using here and, in that pointer, we will pass some arguments.
Going ahead all the processes that are written in here will be followed.
For now, we will not pay a lot of attention to it.
We just have to understand that we have made a display named function already over here.
Now we will go ahead, ahead you can see, we have made one more function named print numbers.
As we had talked about that one function’s point, we will pass it to another function.
So that's why we will need two functions over here.
So here we have made the second function over here, which is called print numbers.
And its return type is also void.
What are we passing in its argument? We're passing one integer value, which we are representing with N U M, that is num. So, this was about our printing number functions.
Now its flow is extremely easy.
In this we have written one printf statement which is only printing the num’s value.
The second function was extremely simple and easy.
There was nothing in it which we might not understand.
So, this was about the two functions.
The first function was display.
The second function was print numbers.
After these two functions, now we are coming to our main part, core of the program, the main function.
Our main function is written from here till here, in this there are all the things that are helping us to run the program.
So now we will see the first thing that is written here is, we have made a pointer here.
And like right now we discussed in our last program how a pointer is made.
Pointing to one function.
Here my pointer's name is pP.
How do I get to know that? P is its name and before that we have put an asterisk and its return type is void, which means it will point towards such a function whose return type will also be void, so you can see here that our print number function’s return type is also void and display’s return type is also wide.
But here there will be one more thing which will differentiate between these two functions that the pointer will point at which function.
So, you can see, ahead in the round brackets, the arguments list.
We have written an integer data type.
If you see here in both the function, the print numbers named function, its argument is basically integer type and it is single, that's why the P named pointer that we are making it point at the print number function.
How will it take the value over here, that we will see here. But we only consider here that we have made P named function whose return type is void.
And in the argument list we have written integer, one single argument which means that it will point at such a function whose argument list there will be only one argument, its data type will also be integer.
So, this is a simple thing of a pointer declaration , function pointer declaration to be more precise.
After this, we will see ahead, there is a printf statement that we have written, what is that printf statement.
The values are, named statements we have just printed.
So, what are we trying to do here? Only 1, 2, 3, 4 and 5, these five values we wish to print over here, that's why we have written in the printf statement, the values are. in front of whatever values are, will keep on printing over there.
When those get printed, we will see them and understand them.
As we are trying to understand these parts of the program.
You can also execute it in your computer and see, you can understand each line from here and type it out in your program on the computer.
And execute and see how the values are coming.
Now we will go ahead.
Here we have also written one printf statement.
After that, we have called this display function, which we have written in the first function definition, we have called the display function.
And you can see that inside that we had written this pointer basically, in the argument we were taking one pointer, now inside this pointer which is the value that we have to send, which value should go in the pointer and get stored.
If we talk about it, since we had to point to a function with the help of a P pointer.
That's why we will pass the function, with which what will happen is, this void start P named pointer that is there, in that as soon as we pass this print number function, our P pointer will start pointing to the print number function.
So, this is something to understand here.
How we have written the pointers here basically.
I hope you are able to grasp it so far.
So, you can see here that in the display we have directly passed the printer number.
So, here if you see we could have also written it in this way.
You could have put an ampersand and print numbers; you would have done something like this.
In this display function instead of printing this, you could have passed it here, this particular pointer.
(typing pause 4 sec) So, here the program could have been written in somewhat this way.
You could have written in this way.
So, with this what we are understanding, our P pointer which is there, which we have represented with the asterisk mark.
In that what is the value that we have stored, our print number with the help of ampersand, we have stored that value.
Which means what we have stored.
We have stored the address of the print number function.
So, we have to access the address over here and how we have passed the address, we have passed it in the display function.
Now it will access from here P and then it will run for loop that was written in it.
As soon as the for loop will run, you could see that we have passed this P and we have passed I in the round brackets.
So, we all know like we had seen in the previous program.
Whenever one pointer is pointing at one function at that time, what can we do? With the help of that pointer, we can use it for the function call.
The same thing we are using here, with the help of P pointer we are passing the value I, which will go in our print number function and in arguments, instead of int num whatever is the value of I at that time, it will come and it will print it over here.
So, this interconnected functions that we have made.
We have listed it in one inside the other with the help of the pointer that has happened very easily over here.
And the pointer has done all these things for us.
So, what we had to do here, we had to get this number printed.
How did we get the number printed? So, the way of printing the number, the logic is written in some other function, so to use that function, its pointers we have passed here as an argument.
And with the help of that pointer, we called that function here.
Now we will execute it and see how exactly it is working here.
First, I will write G C C, after that I will write my file’s name and I will press enter and execute it.
Now this has got compiled, now we will run it.
As soon as we run it, you can see that there is a statement printed here, which says the values are 1, 2, 3, 4, 5.
Somewhat in this way, we wanted it to get printed with the help of these two functions.
And you can see that our print number function which was there, the logic written inside it, where can we use that? In our display function.
But how? Not directly, with the help of our pointer, which was pointing towards our print numbers, which we had written here.
And with the help of it when we pass this P pointer in our display function, that value got extracted and it came here and as soon as we used it as a function call, it got redirected towards this particular function definition and it got printed all those numbers one by one.
So, the first for loop ran when the I’s value was one, here the I’s value came as one and this was a function call and it went on to our print number function.
And with the help of printf function, the num got printed.
So now here for loop increased and I's value became 2 and as soon as it became 2 with the help of printf, even 2 was printed and in this way 1, 2, 3, 4, 5, all the values of I got printed.
So, here we understood how we were pointing a function with the help of pointers, how it becomes more important when one certain function we have to use in another function, call it.
We cannot do this thing directly.
So, we can do this with the help of a pointer.
That’s why here the pointer to a function is very important.
So, I hope you have understood that how a function is pointed and we use the call of the same function, giving a pointer’s name we can call that function and following this process, we can call that function in another function as well.
So, this was about pointers and functions that how pointers and functions can be used together.
If you have any queries or comments, click the discussion button below the video and post there. This way, you will be able to connect to fellow learners and discuss the course. Also, Our Team will try to solve your query.
In the next video, we would be seeing about a few more pointers, like null pointer, void pointer or wild pointer.
What exactly are these pointers and how do they work? All these things we will be seeing in our next video.
So, we will meet in the next video.
Till that time, thank you so much.
Share a personalized message with your friends.