ProgramGuru

Argument labels, defaults and inout

Argument labels, defaults and inout

Swift function calls can read like descriptions of a task. Argument labels are names used at the call site, while parameter names are used inside the body. A default parameter lets a caller omit one argument when a sensible standard value exists.

What you’ll learn

  • Distinguish external argument labels from local parameter names.
  • Use a default argument and an explicit inout update.

Argument labels, defaults and inout in the example

  1. Caller variable

    Swift function calls can read like descriptions of a task.

    Caller variable
  2. Inout update

    An underscore suppresses an external argument label.

    Inout update
  3. Updated caller state

    The call uses label to, while the body calls its parameter score.

    Updated caller state
The call uses label to, while the body calls its parameter score. Omitting amount selects the default 5. The inout modification updates mark to 75. This is a deliberate change to the caller’s state rather than an unannounced global variable update.

How argument labels, defaults and inout works

An underscore suppresses an external argument label. An inout parameter allows a function to modify the caller’s variable for the duration of the call, and the caller marks that argument with &. It requires a mutable variable, not a constant or arbitrary expression.

Work through argument labels, defaults and inout

Save this complete program as main.swift and run it with the Swift toolchain. No separate input file is needed.

Swift · complete program

func addBonus(to score: inout Int, amount: Int = 5) {
  score += amount
}
var mark = 70
addBonus(to: &mark)
print(mark)

Expected output

75

Follow the execution

The call uses label to, while the body calls its parameter score. Omitting amount selects the default 5. The inout modification updates mark to 75. This is a deliberate change to the caller’s state rather than an unannounced global variable update.

Try this program in the Swift compiler. Paste the complete source above.

Practice: Argument labels, defaults and inout

Your task: How do you request a bonus of eight?

Swift · complete program

func addBonus(to score: inout Int, amount: Int = 5) {
  score += amount
}
var mark = 70
addBonus(to: &mark)
print(mark)

Worked answer

Call addBonus(to: &mark, amount: 8). Starting from 70, that call would leave mark at 78. Starting from the already updated 75 would instead leave 83.

Make mutation visible at the call site

An inout access must not overlap conflicting accesses to the same storage. Use a returned value instead when it makes the data flow clearer. Overloads need signatures the compiler can distinguish; different parameter labels can help express different operations.

Check your decision

How do you request a bonus of eight?
Choose one answer. You can try again.

Answer and explanation

Call addBonus(to: &mark, amount: 8).

Call addBonus(to: &mark, amount: 8). Starting from 70, that call would leave mark at 78. Starting from the already updated 75 would instead leave 83.

Make mutation visible at the call site

Call addBonus(to: &mark, amount: 8). Starting from 70, that call would leave mark at 78. Starting from the already updated 75 would instead leave 83. An inout access must not overlap conflicting accesses to the same storage.

Further reading: official Swift language guide.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Closures: pass behaviour as a value →