 Hello friends, I am Sanjay Gupta. Welcome back on Sanjay Gupta Tech School. अब देख हैं C-Language video series अद इस वीडियो में जो तोपेक हम दिसकस करेंगे वो हैं, function. तो function का ये पहला वीडियो हैं, मैंने function को total, in parts में divide किया हैं. तो lecture number 28 में हम function का basic दिसकस करेंगे की, functions कितने ताएक क्योत है, requirement क्या है, कैसे function अंप्लमें कर सकते हैं, और कुछ इजामपल्स, than next lecture over 29 उस में कुछ और programs होंगे जो की functions कर dependent हैं, and lecture number 30 मैं मैं आपको बताँवा की airstream structure को functions के सात कैसे use करेंगे, so three videos को देखने के बाड आप दी जो knowledge है, functions को लेगर वो प्रपरली आपके पास होगी, so three lectures and three videos को दियां से � आपको phile handling के बाड़ें बताँ आता तो उसके भी तीम भीडियो थे, I hope that you have seen the three videos and you must have understood them. And in case phile handling यह फिल्जो function वला वीडियो अभी आप देखने हैं, any doubt or problem created you can communicate with me. So these are my details. So now let's start the function. First let's see the function basically what happens, then we will discuss about their types. So a function is a function that is a collection of one or more than one related instructions which are implemented to perform a particular task. So basically in two types we can categorize functions. One is predefined which are also known as standard library functions and second one is user defined function. So now you will say that you have already used predefined function. So I will say yes. So you have used printf, scanf, exit function, phile handling, fopen function, fprintf, scanf. So all the functions we have used where we have to directly use the function and pass the arguments in it, the logic of that we don't have to design. So all the functions that come in this category are predefined functions. So all the programs that you have been using till date, now you will think about what is the user defined function. So now we have to learn how to implement the user defined function in this video. So for the user defined function, first we have to see the need of functions, the requirement of these functions. So first let me give you a small example. This is our function demo. So now I am not implementing the function. I am telling you the requirement of the function. Why do we have to make a user defined function? So when the requirement will be clear, then you will feel that it is a benefit to make a function and then you will also enjoy learning how to make a function. So I am taking an example of factorial. I have to calculate the factorial of 2-3 numbers. So I have declared n1, then I have declared n2 and then n3. So for example I have to calculate the factorial of 3-3 numbers. So to calculate factorial, we have to apply a logic. So we have to apply that also for i equals to 1, i less than equals to n, i plus plus. And we write f equals to f into i and initialize the f variable from 1. And after that we print that the value of f is factorial. And I have run the loop till n1, for example n1 has 4, n2 has 5 and n3 has 6. So once I have to calculate the factorial of 4, once of 5 and once of 6. So this loop I have implemented, this loop and the print of statement is for n1. It means the factorial of 4 is calculated and it will be printed. Now I have to calculate the factorial of 5 also. So you will say that what is the problem in this, copy the loop and paste it. So we have copied the loop and paste it. So now you will see that it is unnecessary. We have written it twice from this instruction. So you will say that there is no problem in that. You run the nested loop. But if you have to calculate all the factorial at the same place, then the nested loop can run. But it is possible that you have written some statements here. So I have written statements here. And total statements are 20. 20 lines are written here. Then after this line, 20 statements are written here. Means you have to calculate factorial on different locations. So the nested loop cannot be implemented. So there is only one way that you have to copy this logic and paste it where you have to calculate the factorial. So it means we are increasing the number of lines. We are not able to reuse the code. We are just copying and pasting it. So now you will say that the code can be written once, but it can be used again and again. So I will say yes and that is we can do with the help of function. So now you will understand the requirement of function here. To implement function oriented approach. So C basically works on function oriented approach. So that is why we have to make function. Then reusability of code, I have just told you that through function, logic has to be implemented once and we have to use it again and again. And these two benefits, when I will tell you a program, then you will understand how debugging is easy and fast and how compilation and execution is fast. So in this slide, we will come back. First we will see how function works. So that what I have just told you, you will understand it well. So we will modify this. After the gap of 20-20 statements, if you have to calculate the factorial on a different location, then you will have to write the loop once, but the run is more than once. So to make function, you have these three parts of the requirement. The first requirement is declaration, then calling and definition. So first let's make a declaration, let's talk about the factorial. First we have to write the return type, then the name of the function and then the arguments type. So now if I want to make a function, then to make a function, you have to write the declaration on top of the main. This way. So this is the return type of function. After processing the function, a value will provide you with this type. Then what will be the name of the function? And how many parameters the function will receive as input for calculation. So now the return will also be an integer and the receive will also be an integer. If it is more, then how can you write it like this? Okay, the type can be anything, it is not necessary that you use the int. You can pass any type of value. But you have to declare it by time. Okay, so now the compiler will come to know that the fact is going to be used like a function. It means the fact name will not have any variable, only a function. Okay, then let's move on. Next is calling. So wherever you have to calculate the factorial, you can call the function there. So what will you do for calling? The value of the variable will be stored in this variable. Then the function to be called and the argument to be passed. Then I will remove all these loops. Okay, now I have to calculate the factorial of n1. So I will declare some variables here, sorry, f only. f equals to fact of n1. Okay, here f will be printed. Then you have written a lot of statements. After that you have to calculate the factorial again. I will call again, now pass it here. n2, so factorial will be calculated. n2 result will come in f and it will be printed. Okay, then there are 20 statements and you have to calculate the factorial one more time. So what did you pass? So you saw that I have not written any loop anywhere. I have only called the function. So this statement is the function call. Calling of function. So I have done this here also. I have done this here also and here also. I have called the function three times. So how many times have I called the function three times? So I have reused it. So you will say that the loop is not written anywhere. So write the loop also. So for that we have the third part that is definition. So in definition there will be proper statements which will be your calculations. So you have to write the return type, the function name and arguments with their type. So how are they written? So for that you will write int, fact, int n. So look at this calling statement. It will be controlled by calling on the definition. The value in n1 will be n. It means n and n1 are the same. The value in n1 was n. And on the basis of n you can run the loop here. You will write i equals to 1, i less than equals to n, i plus plus. And here you write djf equals to f into i. And when this loop will be over, you will write return f. So this is the definition of the function. And we call this as user defined function. We have written this whole code ourselves. We have written the definition of this whole effect function. So we will decide the task we have to perform in this. So you can see that the loop has been implemented once. And we have called the function definition on different locations. So when this calling statement goes on, you will get the value n of n1. This will be the calculation. And return f will get the value that is written on top of it. And when this statement goes on, this statement will be called. So who will give the value of n2 to the n variable? Then its factorial will be calculated. And the value of f will return to this f. Then the third time this function will be called. This time n3 will provide its value to n. It will be the calculation. And the value of f will be returned to f variable. And it will be written here. So in this way, the input goes to the function. The function will process and return the result. Where the function was called, it will be sent there. So n3, n1, n2 are all the inputs. Who is getting the n variable? And we are returning the output through f. And we are getting the value of f variable. Its name can also be different. And if you think that you can reuse the variable, that is also possible. Okay, so now 4, 5, 6, 3's factorial will be calculated in front of you. So save this program. And when I execute this, you will see that 3's factorial are in front of you. 4's factorial is 24, 5's factorial is 120 and 6's factorial is 720. Okay, so we wrote the loop once. But we called it at different locations. So let's go back to the needle function. You must have understood the reusability. You have to write the code once. Wherever you have to use it, you have to call it. Debugging becomes easy. So if there is an error in the factorial function, then you just have to test the block. You don't have to test the top block. You don't have to test the top block. And compilation execution becomes fast. So why is that fast? Because the number of lines reduced. If you have to write the code less, then compilation time will be less. Compilation time will be less. So your program will be executed soon. Okay, so now if we go to the main slide, then I told you the difference between the two. Till date you were making predefined functions. Now you can make user defined as well. For example, I have shown you the parts and the syntax. So the slide I told you about PPD, these are the three syntaxes. With the help of them, we implemented the program. So I told you by calculating the factorial. Now we will make one more program, which will make you understand the function better. So what we have to do is, we have to calculate some subtraction, multiplication, division and modulus for two numbers. So you will say that there are so many operations. So everyone has to do it in one function. You can make different functions for everyone. So I will say that you can make different functions for everyone. We have to apply operation on two numbers. So every function will take two numbers as input. And then it will perform the calculation and provide the result. So this program will be interesting. So let's erase all this. So now the first function is to be declared. So you have to write int sum int comma int. And then the second function. So the function you have to make, you have to declare it. So I have to make five functions, so I will have to give five declarations. In each function, two numbers as input will be received, so I am writing int comma int. If you want to pass the float, then float comma float. And this is the result after the calculation of the return type. We have to return it. Okay, the name can be any function. But before using it, you will declare it. After that, the main function, now in the main function, we have made n1, n2, two variables. And we have made one variable, res, in which the result will come. And printer, enter two numbers. Let's scan these two numbers from the user. Okay, now what we have to do is call the functions. And after calling, the result will return and we will print it. And the main function will be finished. And then the definition of the five functions. So that we can implement the logic. So the first result equals to sum, in the bracket n1 comma n2. So here n1 and n2 will be input for some function. And the result will come in the res variable. So what will you write? printf sum equals to percent d res. Copy it. Paste it for different functions. So now this will be subtraction. Here we will print subtraction. And the second function will be called. Paste again. So now this will be multiplication. So here we will print the result of multiplication. Then we have to call the fourth function, division. So you have to call division. And here you have to print division. And the fifth is modulus. So mod. So we have called all these functions. Because we have called five functions five times. So in the last example, we had called only one function twice. In this, we have called five functions five times. Now it is not necessary that we have to call the five only once. If we have to call more than one function, then that is also possible. So this statement is called call of function. So wherever I am mentioning the comment, this is calling of function. And this main function is close. Now I have to write definition. So now I have to write this. Because here you can write two things. One is to make a variable. In that, store their result and return it. Now you will say that here A is also written. If N1 and N2 are passing from above, then the name can be different. If we talk about sum, then we will talk about sum. So sum N1 and N2 will pass. Who will receive that? AB. AB is adding them and storing them in the C variable. So return is called C. So who will get the value of C? RES variable. So in the main function, there can be something else in the variable. And the definition you have written below, there can be something else in the variables. And the return keyword is very important. Which transfers value from one function to another function. So you will notice that the main function is also working like a function. So now you must have realized after watching this video, that how can we make more functions in the same program. All the programs that were made so far, everything was done in the main. But this is the first video in which the programs are, apart from the main and other functions, you can also see the logic. Now we have to calculate the subtraction. So copy this. Paste it. So this is the second function. Subtraction. Here we have made minus. Now I was going to tell you one more way. So you can write it like this. You can directly write return A minus B. So the result of calculation is not necessary, to store in any variable and then return it. You can directly use the return statement with calculation. So the result of this calculation will be automatically returned. So if you want to use this approach, then use it like this. If you want to use this approach, then use it as a value. Enough. Now I will paste it once. Then modify it. So this is the multiplier. So this is the multiplier for minus. Then this is the divide. So this is the divide. Then this is the modulus. So this is the modulus. Now in your mind, is it necessary to write it in order? So I will say that you can define it in any order. Now you will say that how it will be executed, how it will be known. So it depends on the calling. I have already called some. Subtraction called. Subtraction, multiplication, division, modulus. So I will run it once and show it. Then I will change the order. So I have put the first number 20 and I have put the second number 10. So look, some has come 30. Subtraction means difference has come 10. Multiplication has come 200. Division has come 2 and mod has come 3. So first sum has come, then subtraction, then multiplication, then division, then modulus. Now I am changing here. I have cut it and I am writing some in the last. So now subtraction will be printed first. See the definition order is the same. In definition some is first, but in calling some is in the last. So from this you will know that calling order matters or definition. So I have put 20 and 10 again. So look, some has come first, then multiply, then divide, then modulus and then sum. So it means that the addition was printed in the last. It means the conclusion is that calling order is important. Wherever you have called the function, it will be executed there. Instead you have defined it anywhere. So in this way, with the help of these two examples, you must have understood that in the same program, how can we use one function more than one? And if we have to make more than one function, which is the user defined function, how can we make it? And this is the declaration. Then the calling statement is this, which is written in the main. And the code we write after the main closes is the definition. So I will show you the syntax of it again. So the syntax of declaration is return type, it is an argument type. So we have to use the data type. Then we use variables in the calling. And in the definition, there are data types and variables. And whatever logic is there. So I hope that you have understood all these things, which I have explained. Now we have created a base that how the function is used. Now if you want to be an expert, then it is very important to watch the next video. Because in that, we have done many other programs and then in return type, we use int, float, carrying, how to use void, then how to print the series of febora, and an important topic, call by value, call by address, which is asked in a lot of exams or interviews. So after watching this, you will understand the function better. And when you will see lecture number 30, you will understand how to use the air string and structure with the function. So after watching lecture number 28 you can become an expert in the function. And if you have any doubt or query, then you can communicate with me. And you will get the playlist link of the entire video series in the description of this video. So you can follow all the videos. Those of you who have seen this video directly, you can watch the old videos so that you can properly explore them. And I hope that whatever I am explaining to you or whatever I have explained in this video will be understood. Thank you for watching this video.