Buckys C++ Programming Tutorials - 39 - Pass by Reference with Pointers

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's going on guys welcome back to your 39 C++ tutorial and in this story we're going to be covering pass by reference with pointers and we actually have a ton of stuff to cover so I don't even know if I'm gonna have enough time in this one store oh I might have to break it up into two different tutorials but let's go ahead and get started anyways so we already know from our last tutorials that whenever we pass an argument into a function passing argument into a function basically means giving our function any additional information that it needs like in order to calculate the square root of a number you actually need a number so you have to pass a value into that function but believe it or not it isn't quite that simple there are two different ways that I'm going to be teaching you right now they're actually more than that but for now I'm going to be teaching you two different ways to pass arguments into functions one of them is passed by value and one of them is passed by reference now what do I mean when I say that what we've been doing is basically passing by value and whenever we create a function and we pass it by value we pass a copy of that variable into the function so the original variable itself cannot be changed until we set it equal to another value so that way the function only has access to the copy of that variable not the original variable itself simple enough values pretty much mean passing in the copy okay now pass by reference means this whenever you pass in a variable by reference you pass in the variables address and whenever you do that you basically give the function direct access to that variable so in that sense the original variable can indeed be modified inside your function so why would you ever want to do that it seems kind of dangerous well the one benefit is it's basically less stress on your computer instead of passing by value where every time you call that function it has to make a copy of the variable suppose you had 100 variables that would be pretty stressful on your computer if you pass by reference and you pass in the value itself it doesn't have to make copies of every variable so let's go ahead and let me start off by making two variables int Betty and I'll set this equal to 13 and let's make another person int sandy and set this equal to 13 to now what we're going to be doing is passing this one in by value and passing this one in by reference and see how they turn out so we're going to be needing to create two different functions first let's go ahead and create a function that we're familiar with pass by value so void pass by value and let's just go ahead and create a basic function it will take a parameter of one integer and what we're going to do is take this integer and set it equal to 99 so basically we're going to be doing is Betty pretty much X is a copy of the variable Betty so then whenever we set X equal to 99 it doesn't set Betty equal to 99 it just sets this copy equal to 99 so that's what's going to happen now let's go ahead and make a function pass by reference so void pass by reference hopefully I spell that right there are way too many E's and efference reference for YZ I mean come on guys enough of these so functions that receive a memory address they need to have a pointer argument because they need to have a pointer because a pointer is the thing that receives the memory address so now instead of in X you need to type int pointer X because the pointer X is now going to be able to hold that memory address so now when I whoa one little extra thing there so now what we can do is something like this points or X is equal to 66 or something like that so now instead of the copy of X being to 99 and now the variable itself sandy X is the address of Sandy so sandy is going to be changed at 66 so whenever we pass Betty in this one her values not going to be changed whenever we pass sandy into this we're basically passing in sandy directly into this value into this function her address and everything so now this is a direct access to the variable sandy so let me go ahead and first of all we need to prototype these functions so copy this and paste got a copy it or did I just do something weird I might've had a extra character somewhere better not get error out so I'm going to be ticked off so copy that and then copy this reference and pass paste this in and now check it out before we go ahead and well let's go ahead and just use these right now and then I'll talk to you guys about the thing I was going to so let's go ahead and pass by value and we will pass Betty into this one so we're going to pass 13 in that but her value isn't going to be changed now let's go ahead and pass by reference sandy and we're pretty much given this function direct access to sandy so that value is going to be changed so now what we need to do is here's what I'm going to talk to you guys about since we're passing in the memory address of course you remember from the last tutorial we need the ampersand because sandy is the variable and this function takes a memory address so the symbol for the memory address of the variable is ampersand sandy this is basically equal to some long hexadecimal address of where that variable is located on your computer so now let's just go ahead and print out the values and see what we get see out arm Betty is now and then just go ahead and print Betty and end the line and now do the same thing for Sandy is now sandy and lime so let's go ahead and run this in I guess I can see if I messed it up anywhere nope looks good so as you can see we basically built identical functions however when we passed in Betty it stayed 13 and when we pass it in sandy even though it was 13 it changed it to 66 so now you can see whenever we pass let me move this down right here whenever we pass a variable into a function we can do it in one of two different ways we can pass a copy of it like we did in the Betty case pass by value and what this does is it basically makes basically X is a copy of Betty it doesn't have any access to Betty itself it's just a variable with the same value so then we set X equal to 99 it's just setting 99 equal to that copy not Betty itself so if we pass by reference what we do is we take the memory address of that variable and remember your memory address is some part on your computer it doesn't make a copy of it it passes in that memory address itself that memory address has the value of 13 and we're saying alright now take that memory address and set it equal to 66 so no copies involved here it's taking that same address the entire time and changing the value of it so now you can see the difference of whenever we use a function like this it makes a copy and we get whenever we use a function like this it works with the direct variable itself so that way whenever we ran the two functions right here and printing it out on the screen that's why Betty stay the same because that copy had no effect on the original and sandy changed because it did indeed change the value of that one address that variable was only a one address and we passed it in so it had direct access to it so I am out of breath and hopefully you guys understand at least the concept of pass by value and pass by reference just remember when passing by value you're passing in the copy when passing in reference you're passing in the variables address if you understand that you're good to go at least for now so in the next tutorial I'm afraid we're not done well I got like Fleming my throat I can't talk I just had a milkshake for McDonald's and anyways moving on so in the next tutorial unfortunately we're not done covering pointers yet so yep more about pointers I'm sorry so thank you guys for watching don't forget subscribe and I'll see you guys in the next video
Info
Channel: thenewboston
Views: 612,285
Rating: 4.9137073 out of 5
Keywords: tutorial, beginners, game, class, program, pointers, lesson, thenewboston, bucky, roberts, computer, string, vector, download
Id: _ja8iizm7nk
Channel Id: undefined
Length: 8min 38sec (518 seconds)
Published: Sun Apr 10 2011
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.