Last time we learned how to write a function that gets valid user input. This time we'll write a function that actually changes its perimeters. Here's the problem we're trying to solve. Functions can only return a single value, and sometimes we might want a function to actually change more than one of the arguments that are passed into the function. Functions use something called passed by value semantics which really just means, if we're passing in two ints, if we're using variables as our arguments, we're not actually passing in those variables, we're making a copy of the values of those variables and that's what gets passed into the function. So even if we change the parameters in the body of the function, we're not actually changing those argument variables, we're just changing stuff copies internal to the function. Our solution to this problem is to use memory addresses pointers to actually change the arguments that get passed into the function. Because we're making those changes on the function side, I'm saying that we're also changing the parameters. Let's go learn how to do that. This is the starting code for this lecture and it doesn't actually do any swapping but we have a couple of values, and we're going to print out their values before a swap and after a swap and I'll show you by running it that they're the same because we haven't even written the swap function yet. Let's write the function prototype first. The return type for this function is void. I am not going to return anything, I'm going to actually change the arguments that got passed into the function. I'll call it swap of values, and we'll have first value and second value. Let's copy this down to the actual function implementation. Flops to two values. Now if you think about how we would actually swap two values, you'll realize that we need a third place to store one of the values as we're swapping them. If you tried to do it without that other location, you'd have to take the second value and toss it into the air and then move first value over into second value and then catch second value into first value. But there is no in the air in computer memory. If we have to store something, we have to store something. We know that variables are the way we store things. So I will declare something I'm calling temp. It's just a temporary variable to help us in this process of swapping the values, and I'll say temp equal second value. Second value equal first value, and if we hadn't store the second value into temp at this moment, the second value would have been destroyed by first value but it isn't because we saved it in temp. So now we can set first value equal to temp. Now I'll call the function with two arguments, value one and value two. I'll run it to show you that it still doesn't work. It doesn't work because the parameters are passed by value. If we had this code embedded here instead, and we used value one and value two, it would work perfectly fine and it would swap those values. But because we have passed by value semantics here, we're not actually changing value one and value two. It's even true that within the body of the function first value and second value gets swapped around. But that doesn't matter because they were just copies of the values in value one and value two. They weren't actually the value one and value two variables. The way we solve this problem is we use pointers. So let's talk about the big idea first and then we'll actually do it. What we really want to do is we can leave value one and value two as the same memory locations. But we want to change the contents of those memory locations to be swapped. Think of it this way. The mailman is standing in front of two apartment mailboxes, and he realizes he put the exact opposite mail into the wrong boxes. So he takes his hand which is temp, and he reaches into one mailbox and holds all that mail. Then he takes his other hand and moves the mail from the other mailbox into the now empty one. Then he takes the mail that he's holding in his temp hand and he puts it in the now empty mailbox. So he's changing the contents of locations. He's not trying to change the locations, but he has to change the contents of those locations. That's what we're trying to do here. That means that we have to be dealing with memory addresses not with actual integers. Let's start by changing the swap values function. Instead of passing an int, we'll pass in a pointer to an int. We'll do that for both of these parameters. So now we're not going to be passing the contents, we're going to be passing the address. Because remember pointers are just addresses in memory. We'll have to do some other changes here. We don't want to set temp to an address, we want to set temp to what's in that memory address. So we know second value is a pointer, and the question is how do we get at what the pointer points to. How do we get at the contents of the memory address. We do that by doing something called dereferencing the pointer and luckily it's just an asterisk. Now we're not setting temp to second value a memory address that wouldn't help us, we're setting it to the contents of that memory or address which helps us a lot. This is the mailman reaching and grabbing the mail out of a mailbox in their temp hand. Now we need to change what's pointed to by second value to what's pointed to by first value. This is the mailman taking the mail out of the box that still has mail into it and putting it into the empty one. Then finally, we say the contents of first value, the stuff that's pointed to the contents of that memory location is set to temp. The mail that's in the mailman is temp pan gets put into the now empty mailbox. So we're doing this by dealing with the contents of memory. We still have some more work to do. We have to change our function prototype, and here when we call the function, we now have to pass in the address of value one, and the address of value two. Because remember we want to pass in pointers. We want to pass in addresses not 5 and 10. We've actually seen passing address of before when we call scan f we have to provide an address, and that's because the scan f function needs to access the contents of the memory location pointed to by a particular variable so it can fill in the value of that variable. Okay, that's a lot. Let's run it. As you can see, it swaps the two values. Now the addresses of value one and value two are the same. They still live in the same place in memory, we've just changed the values that are stored at those two memory addresses. To recap. In this lecture, we learned how to write functions that can actually change the contents of the arguments that get passed into the function. We did use pointers to do that and although we haven't formally studied pointers, we do know that pointers are just memory addresses. We've used the address of operator previously when we called scan f. So the only new thing here was that we needed to dereference pointers, we needed to say tell me the contents of this memory location but that was pretty straightforward to all we had to do is use an asterisk to dereference the pointer to get at the contents of the memory address.