Why doesn't Ballerina let you reassign a value to function arguments?

Viewed 40

The following code snippet gives the error: cannot assign a value to function argument 'a'(BCE2549):

function test(int a) {
    // cannot assign a value to function argument 'a'(BCE2549)
    a = a + 2;
}

What is the reason behind this and can this behaviour be changed?

1 Answers

You can not assign a value to a function parameter in Ballerina. This is by design. As per the Ballerina spec, the function parameters are implicitely final. Which means they are readonly.

If you want to update the value of an input parameter, you have to define a new variable and then update it.

function test(int a) {
    int b = a + 2;
}
Related