 Hello friends, I am Sanjay Gupta. Welcome back on Sanjay Gupta Tech School. अब दिख हैं, see language video series. And in this video, we are going to discuss some more details about the functions. So this is lecture number 29. And if I recall the lecture number 28, then the last video which I made on the function, there was a difference of predefined user define. Why user defined functions are made, how they are made and some examples of it. So in this video, you will get to see some more examples. And in this, we will discuss more details about return type. And if you have any doubt, you can communicate with me. These are my details. So rectify any doubt. And if you want to take online classes, you can contact me. So now let's start. So here you can see that the first problem we have to implement is reverse of a number. So when we discussed the loop, I told you, we will implement the same program through a user defined function. So friends, you must have seen in the last video, what we do in the function is we rearrange the old code. So all the codes you have implemented before the function, you have implemented everything in the main function. But now we divide the main into two, three, four different parts. Like I told you in the last lecture number 28, that we will make different functions for some subtraction, multiplication, division, modulus. So in that video, I told you five functions. So you can make as many different functions as you want. So in functions, you have to rearrange the old programs so that the logic is not written in the main, but in a separate block. And whenever it is required, we call it. So now let's see how to implement reverse of a number. So let's go to the compiler. So this is the program in which I was talking about. I had made five different functions and I had defined the main separately. So now we will modify this. So now we have to do reverse of a number using function. Means reverse is not in the main function, it will be implemented in a different function. First header file. After that you have to declare the function. So I have written int, reverse, int and then main function. And after the main is finished, this is the definition of the reverse function. So in this way your program will be made. The main will have input from the user, the user will display the output. And we will call the reverse function. I have already told you how to call the function in the previous video. So in this, we will revise it once again. And this is the definition of the function. So you can see that in this program, we are making two functions. One is the main and one is the reverse function. The main is also the user defined function and the reverse is also the user defined function. But you have to use the main word for the main. So at least one user defined function whose name is main is not necessary. Now here we will declare the variable. So we have asked the user to enter a number. So the user will input a number and scan it. So n variable will have a number. Now what we have to do is r equals to reverse of n. So we will call this a calling of function. I have called the reverse function. So you can see that in the main function, you will not get the logic of how the reverse is happening. So the result will come in the r variable. So let's display it. So reverse equals to percent d or r. So this is the main function. Now we have to use two variables n and r. There was a number in n which we have to remove the reverse. In the r variable, the reverse will be calculated through the reverse function. Now how will the reverse be done? You can define the logic here. So this is the declaration. Line number 9 is the calling of the function. And the block we are going to write is the function definition. We will implement the logic here. So for the logic, I have made these variables. I have written a while loop that till n is greater than zero, it will be repeated. So this is the logic of the reverse. When I explained the video of the while loop, if you are following the whole video series, you will know how the reverse is done. And if you do not know, you will definitely watch the video series of the while loop. So I have explained this logic properly. So what we did in that? We modded n from 10 to remove the last digit. Then we calculated s into 10 plus a and then n equals to n by 10. So these three statements of the reverse will be calculated which will come in the s variable. Now the value generated in the s is the return key word. So the return key word is whatever value we have calculated in this function will be sent back to where we have called the function. So we have called the reverse. So n will become input. So n will come in this n variable. Then the calculation is done. Return s will throw its value and catch it with r. Variables can be different. There is no problem in different functions. So we have written r here and we have written s at the bottom. So there is no problem. So we have copied our value in the r variable. So in this way we have written the logic of the reverse. So you can see that I have made two functions. One is main and the other is reverse. Now if we execute this program. So when we execute it, I have to input a number and its reverse will be printed here. So it was very simple. We extracted the logic from the main function and made it a new function and used it. Now we have to make the next program Palindrome check. Palindrome you all must know that when we reverse any number and its reverse is equal to the original number then it is called Palindrome number. It means we have to reverse. So you can use this program for Palindrome check. So here I have printed the reverse. You can write if n equals to r. So in n it was the original number and in r its reverse is equal to the original number so what can we print here? Palindrome else we can write not Palindrome. So look at this. We have called the reverse function in the main function. Now you will understand why it is beneficial to return. The return value of the reverse function is in the r variable which we have used for further calculation. So if we want to return the value from one function and use it in another function then that is possible. So I have compared n and r. If it is equal then it will print palindrome otherwise not. So let's execute it. I have put 1, 3, 1 so look at the print of palindrome. Now let's execute it again and this time I am putting 1, 2, 3 so it is printing not palindrome. So in both the cases this program is running properly. Now sometimes it happens that you don't want to return the value. So if you have already studied the function or if you have not studied it then there is no problem. I will explain you something. So the return type of functions will get void. So void if you use return type it means you cannot use return keyword in that function. Why? Because the return type is void. It means in function an argument can be received but after calculation it cannot use return keyword. So in that case what you have to do is you have to cut the logic and we have to implement the logic here. So what happened now? Let's remove this from here. So we have just called reverse function and passed the value of n variable in reverse. So the definition from here will process n. The result will be compared and according to it palindrome or not palindrome will get printed. It means reverse function after calculation will get printed. Why? Because we have used return type void. So I hope you have understood that if we don't do any return then we will have to use return type void. So now it is your choice whether you have to return or not then according to it you can use return type. But remember one thing if we want to get more value then it is not possible then in that case you will have to use return type as void. So that I will tell you in my next example when I will tell you by printing the series of fibronage. Before that let's read this line when to use int float care as return type and when to use void. So int we are already using so we can use float instead of int easily, we can use character and it depends on the calculation. So the use of int float care is the same as any return type you have used. But when we will use void then we will not get any value return. Okay. So now you will have to tell a real-life example where the value we want will not get return and we will have to use void as a return type. So for that I will take an example of fibronage series. So when any function tries to return more than one then it is not possible because one function can get one value return in one time. But your function will generate multiple values. So in that case forcefully you will have to use return type void. Okay. So I have told you this example by converting that using int or void is optional. But there are some cases where you cannot use int even if you want. So you will have to use void. So those are fibronage series using function. Now see return type void name of function fibronage and we will have to pass an int which will tell us how many number of terms we have to get for the series. So we have read a number of users and the function is fibronage. This is removed. Okay. Now we will have to use the variable here. So I have written int i t equals to 0 t1 equals to 1 t2 equals to 0 There are three variables term 1 and term 2 So t is initialized from 0 t1 is 1 and t2 is 0 Now here we will implement a loop which will be n times repeat. So that loop is i and in this loop we will take the logic of fibronage series. So we have to print many values in one time. So we have to print nt equals to t1 plus t2 After that t1 equals to t2 and t2 equals to t Okay. So what happens in this series is you have to print many values and the return keyword can return only one value in one time. So whenever there is a requirement that you are making a function if it generates multiple values if it generates multiple values then don't get return you can print value in the same function So in that case you have to use forcefully return type So this is the function of fibronage series so any series or pattern you have to use any format in the main function you can change the name of the function and you can change the logic in this definition. Now if we execute this it will run properly I have made a mistake here I didn't put comma So I have input 10 so you can see 10 times the number of fibronage series 0,1,1,2,3,5,8,13,21,34 Okay So the program is working properly so I have told you how we can define the function to make a series So I hope you have to use the same function So you will use a simple fund that when we want to return one value and it is the same type of data type but if we want to return more than one value then it is not possible so in that case return type will be void and the results which we have defined we have to print To understand this we will see one more example which is call by value and call by address So first we will see how call by value works and then we will convert it to call by address So for this I will take an example of swapping of two numbers swapping of variables using call by so return type will be void because there will be two variables we have to interchange both so we will have to return two values which is not possible so we will have to input two int comma int we will get two numbers from the user so here I have written enter two numbers or if you do not get the user then I will assign the value if you want you can input from the user there is no problem so the name of the function will be swap and you have to pass two values a comma p so here I have written swap int a comma int b then I have made one more variable c then c equals to a a equals to b and b equals to c this is the simple logic of swapping which you already know and we have passed a and b as an input so we have received a and b and we have made it interchange so from above I have passed a and b and here I have interchanged so I am interchanging both a and b so I will have to return both which is not possible so I will have to print the result here a equals to percent b and b equals to percent b and a comma b so two values of a and b which will be printed in this function after swapping but if I print a and b after swapping, I will print b so I have written a equals to percent b b equals to percent b a comma b so you will see that the below a and b will be printed by swapping but in the main function I have printed a and b after calling, it will be as it is so let's run it first then I will tell the reason see first a and b are printed so a and b are printed so a and b are printed but in the main function what are the results 5 and 10 so what happened above we have written a and b and a and b are used so when we call the function so a and b provide their value so we have swapping after swapping they will be printed here but the effect of this the variables which are made in the main won't be read this is the drawback of call by value if a variable changes its value then it won't automatically modify the other function so either you return the value or print it here so if you want to implement this program properly then make it a comment so you will get an interchanged value but you want the variables in the main also be swapped and the definition below also be swapped so for that we will have to use call by address so what we will have to do is pass these variables which are actually made in the main pass the values of these variables and pass their addresses and point them to receive so here we write m%a and m%b and above you will have to write int asterisk because here we are going to make pointers so a address will come in p pointer and b address will come in q pointer now how the pointer works I have already explained that in my pointer video or if you have assigned any variable's address to the pointer then through the pointer we can access and modify the value of that variable meaning both of them have a link established so now if I write asterisk p then it will process the value of a and asterisk q will process the value of d and from here I will remove it now I will not print anything in the definition I will only print the value of a and b and you will see that we will print the interchanged values because now we have used call by address we have used the address which is implemented through the pointer look at these main a10 and b5 when a was 5 and b was 10 so it means if you do not have to return the value and you have to change the variables in the main then you can implement call by address so you have three types of variations first, if you generate a single value then you can use the return keyword from the function and you can send it to the main if you have more than one value and you are not able to return then you can print it in the definition and third, if you have more than one value but you have to return it in the main function then you can use call by address through the pointer so I hope you have understood everything and now you have understood how to implement the function so implement all these programs in your laptop you can implement any other program and in my next video I will tell you how to pass the array string structure in the function so you will get familiar with it so these are total three videos lecture number 28 in which I covered the basic function lecture number 29 which I explained in this video and lecture number 30 in which we will discuss the function with other features and if you have any doubt you can discuss with me so I hope you have understood the function I explained and if you want to follow the entire video series then you will get the playlist link in the video description so you can follow it so I hope you have understood whatever I explained thank you for watching this video