Table of contents
1.
Introduction
2.
inout Parameters
3.
Example
4.
Frequently Asked Questions
4.1.
How does the inout parameter work in swift?
4.2.
What does _: mean in Swift?
4.3.
What is a good use case for an inout parameter in Swift?
4.4.
What is guard let in Swift?
4.5.
What does === mean in Swift?
5.
Conclusion
Last Updated: Mar 27, 2024

Swift inout Parameters

Author soham Medewar
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

There are times when we need to update the arguments we passed into a function in programming. Swift, on the other hand, does not allow us to modify function arguments because they are set to be constant by default. We'll get a compile-time error if we try to change function arguments within the function's body. It's a form of security that prevents us from changing the parameters by accident.

Let us understand why we need to use the inout parameter in swift using a small example.

Code

var x = 4
func makeDouble(num: Int) -> Int{
    return num*2
}

print("The older value of x is:",x)

var y = makeDouble(num: x)

print("The newer value of x & y is:",x,",",y)

Output

The older value of x is: 4
The newer value of x & y is: 4 , 8

In the above code, we can see that the makeDouble() function takes an integer as an argument and returns the number by doubling its value. But actually, the value of x is not doubled. What if we have to make a function that actually doubles the value of x. Normally, we would do the following thing.

func makeDouble(num: Int) -> Int{
    num*=2;
    return num
}

But the above function will give the following error.

main.swift:4:8: error: left side of mutating operator isn't mutable: 'num' is a 'let' constant
    num*=2;
    ~~~^

To overcome this problem, we use inout parameters.

inout Parameters

Consider the above scenario: we have a function that accepts an integer as a parameter, and we wish to change the double the value of that parameter. These changes must also be reflected outside of the function. We can use in-out parameters in such cases. It signifies that the parameter's value is supplied into the function and then returned to replace the original value of the parameters. In-out parameters use the same syntax as the previous code, with the exception that we must explicitly mention the "inout" keyword just before the data type, and we must use an ampersand(&) before the arguments when invoking the function. The ampersand(&) tells the compiler that we wish to change these values within the function. Another purpose for utilizing the ampersand (&) is that rather than producing duplicates, we want to utilize an alias for the current arguments.

The above programme will be modified in the following manner.

Code

var x = 4

func makeDouble(num: inout Int) {
    num*=2;
}

print("The older value of x is:",x)
makeDouble(num: &x)
print("The newer value of x is:",x)

Output

The older value of x is: 4
The newer value of x is: 8

As we can see that the value of x changes after passing through the makeDouble() function.

Example

Let us take an example where we will swap two integers by using inout parameters.

Code

// Swift program to swap two integers
// Declaring variables
var num1 = 4, num2 = 10

// Defining swap function
func swapValues(_ num1: inout Int,_ num2: inout Int) {
    let temp = num1;
    num1 = num2;
    num2 = temp;
}

print("Values of num1 and num2 before swapping are: ",num1,num2)

// calling swap function
swapValues(&num1, &num2)

print("Values of num1 and num2 after swapping are: ",num1,num2)

Output

Values of num1 and num2 before swapping are:  4 10
Values of num1 and num2 after swapping are:  10 4

In the above code, we are passing num1 and num2 as inout parameters in the swap function.

Frequently Asked Questions

How does the inout parameter work in swift?

The inout keyword is placed directly before the type of a parameter when writing an inout parameter. An inout parameter is a value that is supplied into a function, updated by the function, and then returned out to replace the original value.

What does _: mean in Swift?

The underscore operator (_) in Swift denotes an unnamed argument or label.

What is a good use case for an inout parameter in Swift?

The swap function, which modifies the parameters handed in, is an excellent example. Swift 3+ is an advanced programming language. In Swift 3, the inout keyword must be placed after the colon and before the type.

What is guard let in Swift?

Guard let is a Swift alternative to if let that also unwraps optionals if they contain a value, but it works a little differently: guard let is designed to depart the current function, loop, or condition if the check fails, so any values you unwrap with it will remain after the check.

What does === mean in Swift?

The ===, or identity operator, compares the objects' identities. It determines if the operands relate to the same thing. Despite the fact that the objects are equivalent, arr1 and arr2 do not refer to the same item.

Conclusion

In this article, we have discussed the use of the inout parameter in swift. Also, we have seen the practical implementation of the inout parameter.

After reading about the concurrency in swift, are you not feeling excited to read/explore more articles on the topic of swift? Don't worry; Coding Ninjas has you covered. To learn, see Swift syntax and programSwift environment setup, and Swift protocols.

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc., you must look at the problems, interview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Coding!

Live masterclass