Inside Swift’s inout parameters
Some of Swift’s parameters are not constants. This is why you need to know…
Published in
5 min readJan 7, 2020
Functions usually pass parameters as constants in Swift, which means that they cannot be changed. If you could, you would need to return the result of your changes somehow. This is where the inout
keyword comes into play, and lets you do exactly that.
Difficulty: Beginner | Easy | Normal | Challenging
Prerequisites:
- Be able to produce a “Hello, World!” iOS application (guide HERE)
- Have some rudimentary understanding of functions (guide HERE)
- In a later example, knowledge of named parameters is assumed (guide HERE)
- Ideally understand the difference between pass by reference and pass by value (guide HERE), although the basics are covered within this article.
Terminology
Constant: A value that can not be amended during the normal execution of a program
inout parameter: A variable that is passed to a function, and if changed within the function the the changes are reflected in the original variable outside the function
in-place: On the spot