How to use inout parameters?

Uplift iOS Interview

The Guide is for YOU if
  • You are preparing for an iOS interview and want to improve your skills and knowledge and looking to level up your interview game and land your dream job.
  • You want to gain confidence and ease during iOS interviews by learning expert tips and curated strategies.
  • You want access to a comprehensive list of iOS interview QA to practice and prepare.

All function parameters in Swift are constants by default. That means you can not change the value of the parameters. Trying to change the value of a function parameter from within the body of that function results in a compile-time error. If you want a function to modify a parameter’s value then the parameter should be used as an inout parameter.

The inout parameters can be updated inside the function. The updated value replaces the original value outside the function.

func reverseName(_ name: inout String) {
    name = String(name.reversed()) + ".com"

var websiteName = "ishtiz"

// Output
// ishtiz

// The variable websiteName is changed.

There are some key restrictions that we want to remember when using inout parameters. You can’t use a constant(let) as the argument because constants can’t be changed. We can not set default values as inout parameters. The variadic parameters can’t be marked as inout as well. When you pass inout parameters as an argument you need to use an ampersand (&) directly before a variable. It will state that the function will modify the parameters.

Planning to apply for an iOS job? Check out this article to uplift your resume! Also helpful – SwiftUI and Swift Interview preparation. Happy job hunting!

✍️ Written by Ishtiak Ahmed

👉 Follow me on XLinkedIn

Get Ready to Shine: Mastering the iOS Interview

Enjoying the articles? Get the inside scoop by subscribing to my newsletter.

Get access to exclusive iOS development tips, tricks, and insights when you subscribe to my newsletter. You'll also receive links to new articles, app development ideas, and an interview preparation mini book.

If you know someone who would benefit from reading this article, please share it with them.