 Hey everybody this is Brian and this is our 17th video. Today we're going to be discussing by value and by reference. What do I mean by that? Well if you've been playing around with your own code on the side and just let's just make a little test function here. Let's say int number simple function and what we're going to do here is we're going to say cout number and l. Then we're going to increment that number cout number and l. So all we're doing here is we're actually just incrementing a value and then we want to pass this value over here. So let's say int minum equal 1. Test and we're going to pass it minum. Whoops can't spell today. Let's just copy and paste that here. So as you can see what this is supposed to do is you have a number 1. You're passing it to a function. This function is to simply increments it and then you're putting it through the output. What do you think is going to happen here? Well if you've done this before you know the answer but I'm also going to show you the solution. Let's run this. Starts at 1 it's incremented to 2 but then suddenly it's magically back at 1. What's going on here? Well let's put a little extra information in here just to clarify it. Step 3 we'll say this is a step 1 step 2. So we make our variable. We pass it to our test function and then it goes through step 1. It's incremented step 2 and then finally printed step 3. Run that again. I want you to really understand the structure of this very simple program. Step 1, step 2, step 3. So you can see at step 3 it somehow reverts back to the first value right here. Well what's going on here? Well there's this little thing called buy value and buy reference and what that means is when we pass this variable to this function we're passing buy value. I mean the actual value is passed here. So this is the equivalent of saying number equal 1. So under the hood what C++ is doing it's taking that number and making a new variable called number. That new variable is a copy of minum. It's not the same variable. It's a copy. So now you have two variables in memory both of them equal 1. You're incrementing this variable and then your variable goes out of what's called scope. This is scope right here. This block of code this variable only exists in this block of code. For example if you go to do a C out say step 4 and you try to print that number out well it explodes and says we don't know what that is. When I say we I mean the compiler because it has multiple personalities but that's beside the point. So that scope now that you understand that that variable only exists in this scope you understand now that because it's a copy of this variable it doesn't exist anymore. So in essence what you're doing is you're saying minum and then you're creating a copy of it for this scope and then when you jump back down to here you're grabbing this original copy. Now what you want to do is add the ampersand that's called passing by reference. You're no longer passing a value. You're passing an address in memory. I'm just gonna type some gibberish here. That wasn't even a valid number but you know what I mean. So you're passing a memory address is what you're essentially doing here. So instead of passing a value you're passing a memory address under the hood. So what C++ does is okay this variable is the same address as this variable. Let's run this and see if we have different results here. Sure enough step one, step two, step three. You can see the value is incremented and stays incremented. Remember step three is when we jumped back out of the scope from this function. So let's go through this one more time. I understand this is a confusing concept here. You create a variable called minum. You initialize it to one. You pass it to this function. This function is calling by reference. Meaning this is the address of operator. It's saying give me your address. Where are you located in memory? So the compiler says okay minum is located at and whatever memory address it is and then we're saying okay that variable incremented. Dump it back into memory and then exit out of here. Then when it comes back down to here it just simply references the same spot in memory. That's how by reference and by value work. I understand this is kind of a confusing topic because we are now discussing memory. But it's an important topic for you to understand how this works. So you're going to have some really really weird results. I mean once again if you just simply pass it by value and you try to run it you'll be scratching your head going well why didn't it increment? Well that's because you've made a copy of that variable rather than referencing the actual address. So this is Brian. We talked about by value and by reference. I hope you found this video educational and entertaining. Thank you for watching.