vala Functions Out and Ref parameters

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

Value types (structures and enumerations) are passed by value to functions: a copy will be given to the function, not a reference to the variable. So the following function won't do anything.

void add_three (int x) {
    x += 3;
}

int a = 39;
add_three (a);
assert (a == 39); // a is still 39

To change this behavior you can use the ref keyword.

// Add it to the function declaration
void add_three (ref int x) {
    x += 3;
}

int a = 39;
add_three (ref a); // And when you call it
assert (a == 42); // It works!

out works the same way, but you are forced to set a value to this variable before the end of the function.

string content;
FileUtils.get_contents ("file.txt", out content);

// OK even if content was not initialized, because
// we are sure that it got a value in the function above.
print (content);


Got any vala Question?