In-Out Parameters

In-out parameters allow you to pass a variable from outside the scope of a function and modify it directly inside the scope of the function. You can take a reference into the function’s scope and send it back out again—hence the keyword inout. The only syntactic difference between a normal function and a function with inout parameters is the addition of the inout keyword attached to any arguments you want to be inout. Here’s an example:

func someFunction(inout inoutParameterName: InOutParameterType) -> ReturnType {  // Your code goes here}

Here’s a function that increments a given variable by a certain amount:

func incrementNumber(inout #number: Int, increment: Int ...

Get Learning Swift™ Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.