Question about parameters in a closure expression?

I'm currently learning about closures and how they work and I came accross the following statement on Swift.org in the Closures Documentation:


The parameters in closure expression syntax can be in-out parameters...


I've never head of this concept of "in-out" parameters... what does this mean?



Thank you.

Accepted Reply

inout parameter in a function can be changed in the function and the value kept on return.


The same for closure.


Here is Swift language statement: https://docs.swift.org/swift-book/LanguageGuide/Functions.html

In-Out Parameters

Function parameters are constants by default. Trying to change the value of a function parameter from within the body of that function results in a compile-time error. This means that you can’t change the value of a parameter by mistake. If you want a function to modify a parameter’s value, and you want those changes to persist after the function call has ended, define that parameter as an in-out parameter instead.

You write an in-out parameter by placing the

inout
keyword right before a parameter’s type. An in-out parameter has a value that is passed in to the function, is modified by the function, and is passed back out of the function to replace the original value. For a detailed discussion of the behavior of in-out parameters and associated compiler optimizations, see In-Out Parameters.

You can only pass a variable as the argument for an in-out parameter. You cannot pass a constant or a literal value as the argument, because constants and literals cannot be modified. You place an ampersand (

&
) directly before a variable’s name when you pass it as an argument to an in-out parameter, to indicate that it can be modified by the function.

Replies

inout parameter in a function can be changed in the function and the value kept on return.


The same for closure.


Here is Swift language statement: https://docs.swift.org/swift-book/LanguageGuide/Functions.html

In-Out Parameters

Function parameters are constants by default. Trying to change the value of a function parameter from within the body of that function results in a compile-time error. This means that you can’t change the value of a parameter by mistake. If you want a function to modify a parameter’s value, and you want those changes to persist after the function call has ended, define that parameter as an in-out parameter instead.

You write an in-out parameter by placing the

inout
keyword right before a parameter’s type. An in-out parameter has a value that is passed in to the function, is modified by the function, and is passed back out of the function to replace the original value. For a detailed discussion of the behavior of in-out parameters and associated compiler optimizations, see In-Out Parameters.

You can only pass a variable as the argument for an in-out parameter. You cannot pass a constant or a literal value as the argument, because constants and literals cannot be modified. You place an ampersand (

&
) directly before a variable’s name when you pass it as an argument to an in-out parameter, to indicate that it can be modified by the function.

Check this part of the Swift Book.

Function Argument Labels and Parameter Names (In-Out Parameters)


There's enough explanation and a example. If something unclear for you, please ask.

Thanks. I will also read this in more detail from the Swift Book.