TOC

This article is currently in the process of being translated into Indonesian (~17% done).

Hal-hal Mendasar:

Function parameters

Pada bab sebelumnya, kita sudah membahas tentang fungsi (functions). Kita hanya secara ringkas membahas tentang parameter. Walaupun parameter cukup sederhana dan mudah digunakan, ada beberapa kiat (tricks) yang akan membuatnya sangat berguna.

Hal pertama yang akan kita bahas adalah out dan ref modifiers. C#, dan bahasa-bahasa lainnya juga, membedakan antara dua parameter:"by value" dan "by reference". Setelan awal di C# adalah "by value", yang pada dasarnya berarti ketika anda melewatkan sebuah variabel pada sebuah pemanggilan fungsi (function call), anda sebenarnya mengirimkan sebuah salinan obyek, alih-alih sebuah referensi menuju obyek tersebut. Hal ini juga berarti anda dapat membuat perubahan-perubahan pada parameter dari dalam fungsi, tanpa mempengaruhi obyek asli yang anda lewatkan sebagai parameter

With the ref and the out keyword, we can change this behavior, so we pass along a reference to the object instead of its value.

The ref modifier

Consider the following example:

static void Main(string[] args)
{
    int number = 20;
    AddFive(number);
    Console.WriteLine(number);
    Console.ReadKey();
}

static void AddFive(int number)
{
    number = number + 5;
}

We create an integer, assign the number 20 to it, and then we use the AddFive() method, which should add 5 to the number. But does it? No. The value we assign to number inside the function, is never carried out of the function, because we have passed a copy of the number value instead of a reference to it. This is simply how C# works, and in a lot of cases, it's the preferred result. However, in this case, we actually wish to modify the number inside our function. Enter the ref keyword:

static void Main(string[] args)
{
    int number = 20;
    AddFive(ref number);
    Console.WriteLine(number);
    Console.ReadKey();
}

static void AddFive(ref int number)
{
    number = number + 5;
}

As you can see, all we've done is added the ref keyword to the function declaration as well as to the call function. If you run the program now, you will see that the value of number has now changed, once we return from the function call.

The out modifier

The out modifier works pretty much like the ref modifier. They both ensure that the parameter is passed by reference instead of by value, but they do come with two important differences: A value passed to a ref modifier has to be initialized before calling the method - this is not true for the out modifier, where you can use un-initialized values. On the other hand, you can't leave a function call with an out parameter, without assigning a value to it. Since you can pass in un-initialized values as an out parameter, you are not able to actually use an out parameter inside a function - you can only assign a new value to it.

Whether to use out or ref really depends on the situation, as you will realize once you start using them. Both are typically used to work around the issue of only being able to return one value from a function, with C#.

Using the out modifier is just like using the ref modifier, as shown above. Simply change the ref keyword to the out keyword. In the example above, also remember to remove the value assigned to number in the method and declare it in the call function instead.

The params modifier

So far, all of our functions have accepted a fixed amount of parameters. However, in some cases, you might need a function which takes an arbitrary number of parameters. This could of course be done by accepting an array or a list as a parameter, like this:

static void GreetPersons(string[] names) { }

However, calling it would be a bit clumsy. In the shortest form, it would look like this:

GreetPersons(new string[] { "John", "Jane", "Tarzan" });

It is acceptable, but it can be done even smarter, with the params keyword:

static void GreetPersons(params string[] names) { }

Calling it would then look like this:

GreetPersons("John", "Jane", "Tarzan");

Another advantage of using the params approach, is that you are allowed to pass zero parameters to it as well. Functions with params can even take other parameters as well, as long as the parameter with the params keyword are the last one. Besides that, only one parameter using the params keyword can be used per function. Here is a last and more complete example:

static void Main(string[] args)
{
    GreetPersons(0);
    GreetPersons(25, "John", "Jane", "Tarzan");
    Console.ReadKey();
}

static void GreetPersons(int someUnusedParameter, params string[] names)
{
    foreach(string name in names)
Console.WriteLine("Hello, " + name);
}
This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!