Inline Functions & Default Parameters in C++ Programming

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Yoga is done by up for simple snippets and today in this programming tutorial we will be discussing about inline functions and default parameters so in the previous video tutorial we discussed about the functions in C++ what they are how to use them what is function prototype for this function body and everything about the theoretical aspects as well as we implemented a program which had a function so if you have missed that video you might want to check out the previous video because this is sort of like a part two of that video wherein we are going to discuss different variations in functions as in some type of function as well as how we can add default parameters to the functions so if you don't know what functions are I would recommend you to check that video for styling in the description as well as you can see a pop up over here and with that being said let's get started with today's video so in this video we are going to be talking about inline functions what are inline functions and how are they different from normal functions and we are also going to talk about how to use default parameters so let's talk about inline functions in C++ let me just read out the definition so if function is inline the compiler places a copy of the code of that function at each point where the function is called at compile time okay so this was pretty easy to just mention it in one line we see how it works behind the scenes that is what happens exactly when the compiler runs through an inline function and we will see what how it is different from a normal function so moving at any change to an inline function Google to require the function to be compile because compiler will need to replace all the code once again otherwise it will continue with the old functionality and lastly we have the syntax wherein we just have to add a keyword that is in line before the return type so if you just miss out or don't write this inline keyword this function would be a normal function so the only difference in the syntax between normal function and an inline function is this keyword inline so we just read the theoretical aspect of what an inline function in C++ is wherein the compiler places a copy of the code at every point where the function is called so how does this work in real time or what is the behind-the-scenes scenario so let's take an example okay so as you can see on the screen we have two scenarios we have a normal function scenario and we have an inline function scenario so the what happens when you have a normal function in your program so we have the start of the program from the main function so you can see a written start over here and then the main function get started that is the execution starts on the main function and you can see we have a normal function that is SN 1 being called over here so in this case the control is being transferred to that function so what happens is your main function is at one memory location that is the start of the main function is at one memory location and the function 1 which you have created this is our user-defined function so you've created it and it is being stored at some other memory location say for example this is a memory location ABC and this is a memory location X Y Z so the program counter which started off from ABC had to be transferred to this memory location in order to locate this function definition so there is a transfer of control from this point to this function so this takes a bit of time which is in usually nanosecond but then if there is a huge program which has a lot of function calls then this becomes an overhead after the function 1 has completed execution again the control is transferred back to this main function and then the remaining part of the main function is being executed so this is what happens in normal function wherein there is a control transfer so as I mentioned if there are lot of function calls so a main function is very big and there are many different different functions there is a lot of control transfer being happening which basically adds the overhead and this word is generally in terms of time which makes your program slow so what happens exactly an inline function so when you create a function which is declared as inline so during compile time the compiler checks if the function is in line and if it is in line wherever the function is being called the entire body of the function is being replaced so you can see in this scenario we have main function the function 1 here is in line so the compiler copies the entire function body and replaced it where the function was called so basically this is the place where the function was called but the compiler replaced the entire call by all the code inside the function so what happens during execution time is that there is no enough control transfer so you can see that the control is not being transferred anywhere and since the entire function definition itself is copied in the main function the execution goes in a linear way so this saves that little bit of time wherein the function call caused the control transfer from one address to as you can see over yours so in small programs this does not have a big impact but if your program is big and there are a lot of function goals especially for functions which are very small for example if you want to just check larger number of cou numbers and you create a function that function would have hardly two or three line of code so for that you don't need to transfer the control every time and instead of that you can use an inline function so that would save your time because there would be no control transfer in that case so this is what exactly happens behind the scenes so now we will try to see a program which has an inline function okay so I've opened my dev C++ ID and I have already written down the hash include iostream dot H and all the necessary statements which are needed so we have the int main function you can see over here so quickly open up your ID and type out these line of code and I would recommend that you type along with me so that you get a good practice so what we are going to do is we are going to create an inline function so according to the syntax we need to mention the keyword in line so I say in line and what we are going to create is we're going to create a function that would return the addition of two numbers so I will say in line int add so it is going to return an integer variable so I will say int a comma in B and define the body here itself so that I don't have to perform a forward declaration and if you don't know what forward declaration is I would recommend you to watch the previous video tutorial wherein we discussed what forward declaration means so inside this I will just read one statement I say return a plus B which is essentially the addition of these two parameters that we will be passing and we just add them and we simply return them because we need to return an integer variable in the main function I will just print outer statement is C out addition of four and five is I'll just call the function over here and pass four comma 5 so that's about it this is a very simple statement and save this as inline FN dot CPP let's see if this project compile will execute and I will see compile ok as you can see we have zero errors and zero warnings we got the result so what happened here is this is rarely called the function but the compiler moves this is an inline function because it is needed in line so what exactly is is it replaced the entire function call with this body that is it this entire function itself so this is what is happening behind the scenes of course you cannot see it over here but this is what is happening behind the scenes so there is no function transfer or control transfer from main function to this user-defined add function instead entire function definition and code is being copied and replaced at this position so let's try to run this now we go to execute and run and they were additional four and five is nine now if I would have created this function without inline keyword it would have worked just fine however as I mentioned there is a minor overhead between the normal function and inline function wherein there is a control transfer that is happening which causes some delay in time okay so this was about inline function next thing that we will discuss is about default parameters okay so what are different URLs in functions in c++ so a default argument is a value provided in a function declaration that is automatically assigned by the compiler if caller of the function does not throw out a value of the argument with default values okay sounded a little complex but it is pretty easy so what essentially it means is it allows a function to be called without providing one or word trailing arguments in other words keyword trailing is important when I will explain to you in a while so now you can see a basic function definition wherein we have some of four variables being passed or sum of four parameters being passed but you can notice that two two of these are being highlighted as orange so these are the default parameters have been talking about and what exactly happens is for example you want to often addition of four variables so in this case you can see X Y Z equals to 0 and W versus 0 so when you pass four variables so you say five one two and three all of these would be replacing these parameters but if you pass only two variables so say for example you just want to perform addition of two using the same function this is possible in the case of default parameters so let's try to see an example okay so as created one more file and there is going to be default parameters in functions I have already typed in the essential code so putting word and type this out so now we are going to create a function named sum and it is going to be returning an integer variables else it ends it is going to take four parameters and two of them are going to be defaults I'll send a comma n V comma int C equals to zero and int D equals to five in the function body I will say return a plus D plus C plus D okay so this is our user-defined function whose name is some it is going to return an integer variable it has two parameters which are normal parameters and these two are default parameters so let's try to see what exactly default parameters will help us do so in the main function I am going to call this function some cells you see out sum of 1 comma 2 comma 3 comma 4 is and I'm simply going to pass 1 comma 2 comma 3 comma 4 over here we'll save this as default underscore arms dot c td okay so as i pass these four values one would be taking place of a 2 would be taking place of B then we have 3 hook that would be taking place of C and 4 would be taking place of D so essentially we should get 1 plus 2 Plus 3 plus 4 so let's try to see if this is true I will save this again able to execute compile and run so then go we got the total that is 1 plus 2 Plus 3 plus 4 which is equal to 10 which is correct rate so in normal scenarios even if these two want default parameters this will be true but now what if we just want to pass values for these two a and B and you don't want to pass any values for C and B but you want these value to be default as 5 so in this case this default parameters are going to help you so let me just show you the difference between how it is being done over here I will just copy and paste this else is some of is some is or just some off 1 comma 2 comma 0 coughs 5 is and here what I am going to do is I am just going to pass 1 comma 2 so 1 is going to take place of a 2 is going to take this of B but since we are not passing C and D we are not passing any value which would take C and these place the default values are going to be included so C is going to be 0 because he specified over here that if there is no value coming in from the calling function we are going to take it as 0 and B is going to be 5 so the totals will be 1 plus 2 plus 0 plus 5 which is equal to 8 so let's see if it is true I will just save this you just give a escape sequence and lol save it compile and run so there you go we have sum of 1 plus 2 plus 0 plus 5 which is 8 so this is very different parameters come into picture wherein somehow we want some values to be default if not passed by user so you get an added functionality that is you get the added option that whether you want to pass or not and if you do not pass you can assume certain values according to your requirements one important thing to move down here is default parameters should always start from the right to left that is I cannot give CL 0 in between so this would give me an error if I save this and I'll say compile and run this is going to give me an error because there is one variable or there is one argument to the right of C which is not default so the rightmost value has to be default if you are going to add default parameters or default arguments in function it has to start from the right that is one important thing to note down over here so if I give in D equals to 0 that is make this as a default argument this is valid so if I go ahead and compile and run this would run but now since this is not a default argument I need to pass some value over yourself say 3 you see this you compile and run this would run successfully so very important thing to remember that default arguments are default parameters should start from right to left and you cannot make an argument default argument in between so you cannot go ahead and create this as default parameter because there is a argument to the right of it which is not a default parameter so instead of making this is default argument you can make this variable as default argument so this was all about default parameters so that's it for this video guys I hope you understood the concept of inline functions and what happens behind the scenes and the basic difference between the inline function and normal functions we also discussed how to use default parameters with functions and what they enable us to achieve and which basically adds some extra functionality and gives us options so that's it for this video guys I hope both these concepts if you have any queries you can always put them in the comment section and if you like this video give it a thumbs up share it with your friends and don't forget to subscribe to our Channel please
Info
Channel: Simple Snippets
Views: 65,377
Rating: 4.8887262 out of 5
Keywords: functions and default parameters in c++, inline functions c++, inline functions in c++ programming, default parameters functions in c++, default arguments in c++ programming, c++ inline function, inline function in c++, default parameters functions in c, inline function, inline functions c, c (programming language), c++ tutorial for beginners, simple snippets, basics of c++ programming, default arguments, c++ programming for beginners, inline function in c, c++ programming
Id: fjHz9GPr6qM
Channel Id: undefined
Length: 13min 30sec (810 seconds)
Published: Wed Jul 19 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.