Python Functions, Argument Labels & In-Out Parameters
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.
"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.
Core Rules to Remember



Common Beginner Traps & How to Fix Them
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.
Live Interactive Example
Hit Run Code to see it liveYour Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Calculate Discounted Price Function
Write a function calculateTotal(price: Double, taxRate: Double = 0.08) -> Double.
Return price * (1.0 + taxRate).
In top-level code, call calculateTotal(price: 100.0) and print "Total: $\(result)".
Finished reading and practicing?
Mark this lesson as completed to update your course progress.