Why do we use ouput parameters, e.g.
void f() {
int first, next;
read(out first, out next);
}
void read (out int first, out intnext) {
first = console.read();
next = console.read();
}
Instead of writing all this code why don’t we use:
void f() {
int first, next;
first = console.read();
next = console.read();
}
out (C# Reference)
out
Declaring an out method is useful when you want a method to return multiple values. A method that uses an out parameter can still return a value. A method can have more than one out parameter.
The out keyword causes arguments to be passed by reference. This is similar to the ref keyword, except that ref requires that the variable be initialized before being passed. To use an out parameter, both the method definition and the calling method must explicitly use the out keyword.
Although variables passed as an out arguments need not be initialized prior to being passed, the calling method is required to assign a value before the method returns.