Junior — Middle
Millistel juhtudel kasutatakse funktsioonides inout modifikaatorit?
sobes.tech AI
Vastus AI-lt
In Swift, the inout modifier is used when you need to pass a parameter to a function so that the function can modify the value of that parameter, and these changes persist after the function exits. In other words, the parameter is passed by reference, not by value.
Example of using inout:
func increment(value: inout Int) {
value += 1
}
var number = 5
increment(value: &number)
print(number) // Will print 6
Here, number is modified inside the function, and the new value is retained outside. Without inout, the function would not be able to modify the original parameter.