Hello ji , how are you all? , this is Love Babbar And welcome to the channel code help, this is our lecture no. 30 . Here we are going to discuss some keywords, after using which our code will become more optimal and our code will run fast. In short we are going to be benefited by learning these keywords So, let's start with our first concept, macro. We are going to discuss about macro Here we are going to discuss about #define . Now what is #define ? , With the help of #define we will define 'macro' Bhaiya , first tell me what is macro? ,I will explain you with example you will easily understand it , don't worry This is resembling like #include.The way we write in code, #include<iostream> , I have already explained why we write it Why we write this? ,do you remember or forgot? , if you remember this then you are on right track , if not then you are going on wrong track. This is your preprocessor directive , what do you mean by that? It means before compilation of your program , whatever file your talking about will be copied in your code ,and the code you are using will be referred from there there is no need to worry. Give some example Bhaiya ,here I have used cout somewhere in the code , the definition of 'cout' is written in iostream So, we have to include iostream .Therefore , this is preprocessor directive which includes iostream file , and brings it to your source code now you will use 'cout'.This is now feasible for you because you have copied the iostream to your source code In layman term I have explained you , in same way using #define you can create a macro Bhaiya What is macro? , wait I will explain you with example Wherever you go , you are going to get the same example. I have written a code where I have used 'pie ' multiple times Area of circle = pie *r*r = 3.14 *r*r , when I find surface area of cone=pie*r*l= 3.14*r*l .Therefore I have used pie value multiple times in my code Assume that we have used '3.14' , 10 times in our code, got it. Let's assume that i future we find that this '3.14' value calculated mistakenly ,the right one is '3.29' . So before moving on this video, lets talk about Coding Ninjas to deliver this free DSA course to you , from quality expertise to video sponsorship has been supported by Coding Ninjas. So that you get this course, thanks to them Coding Ninjas is the largest coding education platform where you can take different types of courses, if you are interested in paid courses if you want to do development , machine learning , android , web dev , data science , core subjects OS, DBMS, DSA or if you want to do Java ,Python or any other language , then you can take the paid course from here the lovely thing about Coding Ninjas that is being praised for their one on one doubt solving support. If you have any doubt , then that will be resolved in an hour. The courses here are well structured , if you work hard then you'll definitely get the result . The ones who made this course are already placed in big companies so you don't need to take tension about that. If I talk about the language here, its available in both Hindi and English, you will also get hint videos while solving the questions So if you want to buy any of the paid courses , then you are intelligent the link is in the description , you can take 20% discount by it , which is also the maximum, you will get it through this link , THANK YOU ! now we have to go 10 places , and we have to replace '3.14' with '3.29' . is it a good practice?. I s it feasible? Now lets assume if I have done it in 100 places, then I have to go 100 places, same for 1000 places if your code base is 3 years old, you have written 10000 times '3.14' .So , will we go to 10000 places to change it . NO! Bhaiya it is simple. Why you are making us fool?, you can do this way , initialize a variable 'double pi=3.14' and use it everywhere ,now if you want to change it , change it here. Now if I have written this, 'double pi=3.14'. What are the problems here? lets understand it I said you have taken storage here for creating this variable ,m it has gotten memory block which name is 'pi' , contain 3.14 So you have used storage here my friend, perform will get affected can I optimize it, can I do something so that I don't use memory , lets see We understand this, lets assume if someone changes this value then what can you do? Did you got it? So want can we do, I have told you about #define by which you will create macro, you can write this way #define PI 3.14 , there is no need of ';' And this way you have created a macro , here you have created a PI named macro Now lets see what is macro? Macro means a piece of code in a program that is replaced by value of macro ,lets assume this is your code Here , we have written int area= 3.14* r* r , r=5 this way we found the area and then printed it ran it here I got the answer ,did you got it Ok now the 1st solution you suggested , 'double pi= 3.14 , we have discussed this , here we replaced it with PI now run it ,you again got the answer now here you created a macro there is no need to write pi, this way we used macro now what will happen here code wise , lets see when I create macro like this lets understand this , here we written PI ,before compilation every place PI is written will be replaced by 3.14 It means if it is written pi anywhere ,it will get replaced by 3.14 before compilation ,then it will be compiled now you do not need to take extra storage and also there is no need to allocate memory , you are replacing it before compile time , it is also not going to change here you can't write PI =PI+1 ,lets see by running it once then you will get what I am trying to explain you can't update it here ,you can't change it or update it and you are not taking extra storage if you have written 'double pi=3.14 then you can write this . It will run perfectly But now you have used macro , you can not change it , until you change this value , 2. you also not used any extra storage But if we talk about line no.9 , here you have used storage of 8 bytes but in line no. 4 you have not used any storage before compilation , here you just replace this pi with PI . This is called 'macro' . This is the logic , lets go through it again ,Macro means a piece of code in a program that is replaced by value of macro ,lets assume this is your code As I have written Pi here it got replaced by it value It made easy for us to maintain the code, no one is going to update this and also no extra memory is used. There are many benefits. in homework ,go to this article of GFG ,Macros and its types and go through it . Some types are given you have to read it Try to understand it , try to spend more time at this example So you have to read this article of GFG, and you have to spend atleast 10 min here I 'll say before continuing this video , first go through this one . Here we completed Macros Lets move to our next topic , Global variables when it comes in our mind about 'global variables ' , let me give you a situation I have a variable and I want to share it between functions How ? , let me right an example Here I have some functions ,here I made a function void a() ,2. void b() and you already have a main function . I want to share a variable between main() and a() So , I created int i=5 , it is created in main() now if I want to give it to a() . So , I will call a() by passing 'i' as parameter here int i and then I printed it Bhaiya you are doing wrong . What ? Lets see , here you have passed 'i' as 'pass by value' ,you created a copy here. now if I want to share it, I will make it reference variable then I will share it lets call b(i) here , I have to share it also then will print it So if I want to share it , I have to use reference variable ,I can share it Is there is any other method we can use global variable through which you can share When I say we can share it , it means that your memory block should be one only 'i' is created only once but it can be accessed by many functions ,memory block will be single , it will be created only once , can be access by many functions lets see local variable , I have already explained you just revising it . As I said here inside main() we created int i , this 'i' is a local variable for this main function if I create one more char ch ='a' this is local variable for a() , when we exit the function it gets destroyed in same way when it I gets over int i will be destroyed , got it . So local variable has life span within this blocks and write int i=2 and then print it I will comment it out and lets run it what's the problem ? , comment it out also . Here we got 2 In short , the 'i' which you have created it will exit in this block , here you have created a block explicitly , you have created a new 'i' here So, when you will access 'i' , then this 'i' will get accessed if I tried access it outside , then here 5 will get printout . you already know about scoping , this 'i' can be access here and this will be accessed within this . Got it In same way , I want to create a variable which life span is throughout the program , int score = 15 So I can use this score anywhere in the program , if I write it here cout<< score<< " in a" << endl; , I used in a() ,b() and also in main() Now I ran it , we can see it has printed 15 in main , 15 in a , 15 in b . Therefore the score which we have created here can access everywhere Global variable is that variable which has scope globally , all function in this program can access this variable , it is allowed We have understood it This is called Global variable , this is all about scoping , but bhaiya should we use it I said don't use it ,this is bod habit . Then bhaiya why we learned it I have to tell every concept , if I will not then you will say I haven't taught you this but we will not use it , why? . Because its a bad practice its disadvantage is that , anyone can change it if any function changed it , then for all other function the value will get changed If here I changed the value of score here, I incremented it and ran it it printed 15 in a, and 'a' has incremented it , in 'b' it has become 16 . Therefore, any function can update this global variable , this change will be reflected everywhere So this is a bad practice , and will not use it . Then what should we use? when ever it comes to share a variable ,use the concept of reference variable This is the technic , it is very simple Lets move forward to our next topic , which is ' inline functions ' lets start , what is inline functions ? , lets understand it if I give you a line , by that you will get its understanding Here we have written , inline functions are used to reduce the function calls overhead , what do you mean by that ? It means , anywhere we are calling function multiple times and we have found a method to skip this process Which is called inline function when we do function call ,one more function gets added after main() in stack performance is decreasing a little bit , let me explain you properly let say , I have created a function , void func(int a, int b) , here we did a++ , b++ cout<<a<<b<<endl; here I wrote func(a,b) ,and here I have initialized int a=1 , b=2 in this way we did function call there is a stack space ,main() is in the starting , it is containing a, b which is containing 1,2 respectively . when you call this function at line no. 16 , then this function will get entry , where you have again created 'a' and 'b' , so here you have allocated a new memory when you call a new function it takes time ,and you also used memory performance has definitely reduced how can I prevent it , is there any way to avoid this time and space , lets understand through example let say , I have some values a=1 , b=2 if a greater than b, or let make a ans here and let it equal to 0 and I filled 'a' in 'ans' else in ans we put 'b' , in this way But I can write it in a more simple way by using ternary operator Now lets understand what is ternary operator ? If I write this way , if( condition) , if it is true then do this and if false then do this now I can write it in a single line , lets see how ? there is a ternary operator , this is your condition and this is syntax , after this write , which is true then which is false Now if I want to change this whole thing into ternary form , we will write ans =(a>b) ? a:b I have converted this whole statement in this form I will explain why we are doing like this ,when you moved forward , a=a+3 and b=b + 1 now I have changed the value , again repeat the same thing , I have written this thing multiple times readability wise it is not looking good , I have to think every time about it So I have to write it in more efficient way, when one thing is repeating multiple times , then we put it in a function We made a function int getmax(int a, int b ) , and I put this thing here , good . This way I created this function Here I write ans =getmax ( a, b) , in same way here also , now is there any problem here we are creating copy of a and b , memory is allocated for this you asked Bhaiya can I use reference variable ? , do it let run it one time we ran it , ok . you got 2 and 4 as answer So , here we used reference variable to save the memory but function call is still happening Here come the role of inline function , I can make tnis function an inline function . lets run it Answer is still same Now I will explain what is the role of inline function ? ,first see the condition this is function and this is the body , if your functions body is of one line , then compiler suggests to make it inline if your function is of one line , then compiler will treat it as inline if it is of 2 or 3 line then it depends upon compiler if it is greater than 3 then compiler will not treat is as inline Bhaiya what is inline function ? , explain it In macro pi always gets replaced by 3.14 before compilation , same thing is going to happen Before just compilation , wherever this is written , it will get replaced by this one , got it This is called inline function , if you have considered it as inline function and compile approves it wherever the function is being called will get replace by this What's the benefit ? It is increasing the readability when the code is getting executed , this get max is getting replaced by this and my performance is also increasing . No need to create new storage or calling function ,got it . No extra memory uses ,no function call overhead , this is your benefits condition is in front of you , I have explained you lets assume ,here I have written a big function and converted it to inline , will it work ? I have already told you , as soon as you will go greater than 3 , compiler will not accept it I hope you have understood it completely So , this was inline function , where your function call gets replaced by function's body Lets move to our next topic , Default arguments It is is very simple and nice topic , if I say I have never used it practically in my life , but we should have some knowledge about it lets clear it . We have a function lets assume I have an array 1,2,5,7,9, like this let me show you , int arr[5]= { 1,4,7,8,9} like this I created a function print (int arr[ ] int n, int start) , then I ran a for loop from start to n-1 , and printed array What is wrong here ? , tell me quickly We have created a function which from print array from start to last here I called the function print (arr, size, 0) ,will start from 0 lets run it , and it printed 1,4,7,8,9 . Good!! Can I do something , that I don't know the 3rd argument , then you will print full array I want to say that , here I am passing array , size , start I want to make 'start' argument optional , if someone passes it then print according to that , and if not then print the whole array So, here I want to make this argument optional . We were talking about default argument int start =0 , here I made start a default argument , if someone didn't passed start then it will take 0 as default If I only gave array and size and I didn't gave start . You can see it is not showing error So when I don't give it start's value then also it runs But now I want to give the value of start 2 , so it is printing 7,8,9 lets write neatly for you , lets run it again So in default argument you created this type of argument , this argument is optional . here take 1 or 0 , whatever you want . Lets understand it once again in default argument you go to the function's signature and initialize the value you have made the argument optional ,If someone gives the value then , will use it , if not just like in line no. 17 then ,will use the default value This was your default argument Always take care about a condition ,default argument will always start from the the right most parameter It means , it never going to happen I made this as default before this one . This is not allowed You can see by trying it . I ran it . See it shows error . therefore it is wrong way to do We should always start from the right most , after that you make 'n' also a default argument . Its upon you So always start from the right most , if there was one more variable , int end =0 ,this game would have went like this You have to first make the right most as default , then only you can move to others I expect you have understood it properly if you want , you can set all parameters as default We have understood it all ,now you can create default argument In future when we will learn inbuilt or predefined function , then you will understand that here we are using default argument If I have explain it in Hindi , then I will say , if you want to give then give it or I will assume it 0 So print function is instructing main() to , in my function call if you want to give start then give it , if not then I will assume it Simple . So we have learnt four topics in this lecture Macros , Global variables , inline functions and default arguments All these together makes your code more fast ,better and increases the readability So take a look on these , do practice and you will start understanding it better Do comment in the comment section , lecture is short , tell about you experience about this video , will meet in the next video I will give you one thing before moving forward , in homework Our next lecture will may be on constant variable, you have to complete it in the homework read it once before coming to the lecture If you want documentation reference, I will link the article of GFG , till now we have studied dynamic memory allocation ,macros , global variables Open it , and read it , you can understand it from here Advantage disadvantage , everything is given here and you will start understanding it . Constant variable is also there , read kit also If I felt the need of a video on this , then I will make it or will start recursion Go through this in code studio That's it for this video , will meet in the next video , till Bye bye!! do comment in the comment section how you felt about this video , so that we can improve This is lecture no. 30 . Will meet in the next video , till Bye bye!!
Get free YouTube transcripts with timestamps, translation, and download options.
Transcript content is sourced from YouTube's auto-generated captions or AI transcription. All video content belongs to the original creators. Terms of Service · DMCA Contact