O'Reilly logo

Mastering Swift 4 - Fourth Edition by Jon Hoffman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Inout parameters

If we want to change the value of a parameter and we want those changes to persist once the function ends, we need to define the parameter as an inout parameter. Any changes made to an inout parameter are passed back to the variable that was used in the function call.

Two things to keep in mind when we use inout parameters are that these parameters cannot have default values and that they cannot be variadic parameters.

Let's look at how to use the inout parameters to swap the values of two variables:

func reverse(first: inout String, second: inout String) {  
  let tmp = first 
  first = second  
  second = tmp 
} 

This function will accept two parameters and swap the values of the variables that are used in the function call. When we ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required