Coding/Learn/swift/swift functions and argument labels
Swift Foundational Course
01.Introduction, Safety Philosophy & The Toolchain
02.Constants (let), Variables (var) & Type Inference
03.Strings, String Interpolation & Multiline Literals
04.Operators, Range Types & Nil-Coalescing
05.Control Flow: If, Switch Pattern Matching & Guard Statements
06.Optionals, Optional Binding & Optional Chaining
07.Arrays, Sets & Higher-Order Collection Operations
08.Dictionaries, Default Lookups & Tuples
09.Loops, Defer Execution & Labeled Statements
10.Functions, Argument Labels & In-Out Parameters
11.Closures, Trailing Closures & Shorthand Arguments
12.Enums, Raw Values & Associated Values (Tagged Unions)
13.Error Handling: Throwing Functions, Do-Catch & Try Options
14.Structures (struct), Value Semantics & Mutating Methods
15.Classes, Reference Semantics & Inheritance
16.Automatic Reference Counting (ARC), Weak & Unowned References
17.Properties: Stored, Computed, Observers (didSet) & Lazy
18.Protocols, Protocol Extensions & POP (Protocol-Oriented Programming)
19.Generics, Type Constraints & Associated Types
20.Capstone: Production Async/Await Cache & JSON API Engine
Course Certificate0%

Complete all lessons to earn

LearnFunctions, Argument Labels & In-Out Parameters
10 / 20 0%
Lesson 10 of 20
9 mins read

Functions, Argument Labels & In-Out Parameters

PreviousNext Lesson
In Plain English

Swift functions are designed for expressive, English-like readability at call sites. Learn parameter naming (argument labels vs parameter names), omitting labels with underscore (_), default parameters, variadic inputs, and mutating caller values with inout.

The Mental Model

"Argument labels make calling a function read like an English sentence: greet(person: "Alice", from: "Cupertino") sounds natural and makes code self-documenting."

Deep Dive: How It Works

Argument Labels vs Parameter Names: func greet(to recipient: String) -> String; to is used at callsite, recipient inside body.

Omitted Argument Labels (_): func calculate(_ a: Int, _ b: Int) allows calling calculate(10, 20) without labels.

In-Out Parameters (inout): Passes variables by reference (copy-in copy-out); callers must prefix arguments with ampersand (&).

Variadic Parameters: func sum(_ numbers: Double...) collects arbitrary arguments into an array [Double].

Syntax Blueprint

func swapValues(_ a: inout Int, _ b: inout Int) {
    let temp = a; a = b; b = temp;
}
swapValues(&x, &y)

Use inout for mutating parameters and prefix passed variables with & at the call site.

PointCore Rules to Remember

Point star
Readable Call Sites: Argument labels clarify parameter intent directly at the invocation point.
Point star
Default Parameters: func connect(host: String, port: Int = 8080) allows omitting parameters with defaults.
Point star
In-Out Reference Semantics: inout parameters allow functions to mutate the caller original variable directly.

Common Beginner Traps & How to Fix Them

Mistake:Passing a let constant or literal to an inout parameter (e.g. swapValues(&10, &20)).

Why it happens: inout requires mutable variable storage (var) to write back the mutated value.

How to fix: Ensure variables passed to inout are declared with var.

Pro Tip: Use _ for the first argument label when the function name already implies the argument (e.g. print("Hello") rather than print(item: "Hello")).

Live Interactive Example

Hit Run Code to see it live
Argument Labels, Default Values and In-Out Swap
Swift 5.10 / LLVM
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Output Console
Click "Run Code" to view the rendered output.
How it works: buildGreeting read naturally at the call site, and inout swapped x and y in place.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Calculate Discounted Price Function

  • PointWrite a function calculateTotal(price: Double, taxRate: Double = 0.08) -> Double.
  • PointReturn price * (1.0 + taxRate).
  • PointIn top-level code, call calculateTotal(price: 100.0) and print "Total: $\(result)".
1
2
3
4
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates