From the course: Learning C#

Reference and out parameters - C# Tutorial

From the course: Learning C#

Reference and out parameters

- [Narrator] The C# language has some interesting, unique features, and we're going to look at a couple of them in this example. So let's open up our RefAndOutParams folder, and we'll open up the program code. So ordinarily, when you pass value arguments to a function, that function can't change the values of those arguments outside the function. So let's see an example of this. Here I have a function that takes a single integer argument. And inside the function, if I add some code, and I'm going to modify the argument, so let's do something like arg1 plus equals 10. So we're going to increment arg1 by 10. This change is only temporary inside this function because the function receives a copy of the value in the argument when it is passed in. So down here in the main function, when I call this function in main, and I print out the result, the value doesn't change. And we'll see that that's the case when I run the code. So let's go ahead and bring this up in our terminal. And let's run this. So you can see that val1 starts out as 10. So I call the function with the parameter here val1. Inside the function, it gets added to 10, so that result is 20, but outside the function, val1 hasn't changed. It's back to 10 again. So now let's make a copy of that function. And let's name it TestFunc2. Now what I'm going to do is I'm going to put the keyword ref in front of the argument. So now, instead of just saying int arg1, I have ref int arg1. And I'll also make a copy of the calling code. And we'll paste that here. And I'll call TestFunc2 with val1. Now also you'll notice that there's an error here because it says argument one must be passed with the ref keyword. So to make the code easier to read and make it explicit what's going on here, I have to call it like this. So I'm going to call it with ref val, and you can see that when I do that the little red line disappears and there's no more error. So what this keyword does is it indicates to the compiler that the argument is being passed as a reference, instead of a copy. So now the function does have the ability to change the value, and have that change be reflected outside the function. So let's run the code again and let's see what happens. So here's our first example, where the change only happened inside the function, but here you can see that when I change the value inside the function, the change happens to be reflected outside the function as well. So by adding this keyword, you can give functions the ability to change the parameters that they're given. There's one other interesting keyword called out, which specifies that a parameter is used to return a value instead of supplying data to the function. And we saw a very quick example of this earlier when we were working with the strings chapter. So let's go ahead and add our example here. So what I'm going to do is we're going to define a function, and that function is going to be called- Oops. So we'll call it static void. So we'll have the function, not return a value, and we'll call it PlusTimes, and it's going to take an int and an int, so it takes two integer arguments and then it's going to return, the sum, and product. So the out keyword in this case means that the function doesn't expect to be given a value here. These two parameters are going to be used to return values back to the caller. So inside the function, let's have sum be arg1 plus arg2, and let's have product be arg1 times arg2. So then when I call this function down here in main, once again, let's define two integers. So I'm going to call PlusTimes, I'm going to call it with val1 and val2. Now, when I call the function again, I have to specify that keyword. So I have to say out a and out b. So a and b are going to receive the values, the sum and the product of val1 and val2. And then we'll just print the result. So let's go ahead and run our updated code. And here in the output, you can see that the sum of 20 and 20 is 40 and the product is 400. So you might be wondering like, why do we use things like out parameters and rough parameters? So the main purpose of the out parameter is to enable a function to return multiple values. And you might see this in older C# code. However, this isn't really the recommended way to do this anymore. So while you might see this in older C# code, there's a newer way to return multiple values, and that's by using what's called a tuple structure. And we'll see how to do that in another video.

Contents