In this article, we will learn about in-out parameters. They allow us to modify variables we pass as parameters in a function.

Let’s quickly explore them in an Xcode Playground.

Let’s Start

Consider the following simple array of String values:

import Foundation

	var values: [String] = ["hello", "world"]

We want to make each member of the array uppercased, so we write the following function:

func transformValues(_ values: [String]) {
	    values = values.map { $0.uppercased() }
	}

However, we get the following compiler error:


The values parameter is a constant, how can we make it a variable? The answer is to use it as an in-out parameter:

func transformValues(_ values: inout [String]) {
	    values = values.map { $0.uppercased() }
	}

Now if we run this function (note we use the ‘&’ symbol when passing a variable as an in-out parameter) we will see the output we want:


Wrapping Up

Interested in more lesser-known features of Swift? Feel free to check out my other relevant articles:

#swift #mobile #programming #technology #ios

What are In-Out Parameters in Swift?
1.10 GEEK