C++ Programming Tutorial 67 - Pass By Reference and Pass By Value

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
you know what is going on everybody this video we're going to be talking about passing by value and passing by reference specifically we're talking about passing arguments to functions so make sure you watch my videos over function so you understand the general idea and then you should be pretty good following this video now this is the concept video the code follow-up is going to be in the next video so if you're looking for a hands-on example make sure you get through this video to understand how it works and then check out the upcoming video but before you check out the upcoming video you got to check out our sponsor which is Embarcadero rad studio rad studio is the IDE of choice for C++ development quickly build native mobile and desktop applications from a single C++ code base and deployed to Windows Mac iOS and Android with rad studio user interface design has been made easy with hundreds of pre-built components for cross-platform development you can easily integrate with popular source control management systems databases api's and you can make your life easier with numerous third-party extensions let rad studio do the heavy lifting when it comes to C++ development give it a go with a free trial by following the link in the description now let's say we create some function and we take some argument let's just start with an integer when we call this function in our code we pass a value to this function so we'll first go through an example when we just pass a value such as 5 well this 5 is going to be copied into this variable which can be used inside the body of the function now what if you do a variable here instead of a value so let's say at some point we define some variable we can name it the same thing as the function but it doesn't have to be named the same thing they are two separate variables but let's just call them the same thing in this situation so we have some variable that was defined in two x equals five and then we pass in X well here's the thing you need to understand is that when you do this the value 5 is copied into this variable so that means somewhere in memory we have two variables they both happen to be called X and they both have the value 5 but they're two distinct variables if we change X inside of this function and we say X now has the value 13 well what this is going to do is it's going to replace the value here and it's not going to affect this original variable so that means after this function called down here X is still going to have the value 5 and that is a very very essential concept you need to understand when it when we're talking about functions inside a C++ everything is passed by value so this whole concept is known as passed by value now the one exception of this is a race so if you create an array and I'm hesitant to draw it out because I'll probably just mess everything up if you create an array which contains multiple elements when you pass an array to a function it's not passed by value if it was passed by value all of the values in the array would be copied to this new variable but instead it's passed by pointer so what that means inside a memory we have an array that contains some data and when you pass that array to the function it gets a pointer to that memory location so what that means is let's say this was an array that variable now points to the same array so that means any changes we do to the array inside of the function are visible on the outside so so far we talked about basically two ways of passing variables one is passed by value which is the default for everything the other way we talked about is passing by pointer and more accurately it would be said as decaying to a pointer so when you pass an array let's say this is not an integer it's actually an integer array and we take that array and pass it to the function it decays to a pointer meaning it loses its type information so inside of the function it no longer understands that it's an array of a certain length it only sees it as a pointer to the first element so this is just going to point to that first element here inside of the array okay so that's a lot of information now there is a third way of passing in from and that is passing by reference now passing by reference is very similar to the way arrays are passed but it's not something that happens automatically we can't change it's something we have to say that we want to do so if we want to pass something by reference it's similar to how an array decays to a pointer but the difference is that we manually do it and it doesn't lose any type information so let me explain and in the next video we'll go over some hands-on examples it'll be a lot more clear let's go back to the beginning we have an integer variable X another integer variable X and then we call the do stuff function and we pass in X well if we want to be able to change that variable inside of this function we need to pass by reference and the way we do that is we prefix the variable name with the + sign which my and sign drawing abilities have skyrocketed since the start of this YouTube channel so I'm making some serious handwriting progress so it's going to look something like this now you might see the + sign here you might see attached to the type doesn't really matter as long as it's between integer and X you should be good to go now in this situation this X here and this X here are going to refer to the same area of memory so if we pass in X value of 5 and then inside of here we assign the value 11 well this is going to be replaced with 11 and after this function call X will now be valued at 11 so that is passing by reference this is important to do if we have very large data structures or if we want the functions to be able to modify the variables so this is very common with vectors if we have some function that we want to alter data inside of a vector we need to pass that by reference that's because only arrays are going to be changeable inside of a function by default anything else we need to pass by reference so this X is essentially an alias or in other words a nickname for the same variable it doesn't have to have the same name here you could name it Y it doesn't matter you need to know is that when you change this variable you are changing this variable alright so that is your basics for passing things as arguments the very first way to do it is pass by value which is the default for everything except arrays now this is even if you're using a custom class type so if you have an object of a custom type that is still going to be passed by value which is actually different than a lot of other popular programming languages and c-sharp for example objects are going to pass by reference by default not the case in C++ so everything is passed by value except arrays which decay to pointers then the third type is passed by a reference with you if you're working with anything that's passed by value and you want to be able to change it inside of the function then you need to pass by reference by prefixing it with the + sign alright so that is all I have for you in this video please consider subscribing if you've enjoyed this content and if there is anything else you'd like to add or anything you're concerned or question about leave a comment in the comment section below and I'll see what I can do thanks and I'll see you [Music]
Info
Channel: Caleb Curry
Views: 36,169
Rating: 4.8904595 out of 5
Keywords: c++, programming, tutorial, pass, by, pass by, reference, value, Pass By Value, caleb, curry, calebthevideomaker2, code, coding, intermediate, beginner, computer, science, c (programming language), pass by reference, pass by value
Id: FXzpFn8LJUI
Channel Id: undefined
Length: 8min 7sec (487 seconds)
Published: Tue Mar 19 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.