0Pricing
Swift Academy · Lesson

Default and Variadic Parameters

Setting default values and accepting variable numbers of arguments.

Default and Variadic Parameters is a free Swift Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Welcome

Default parameter values let callers omit arguments they don't need to customise. Variadic parameters accept any number of arguments. Both reduce boilerplate and improve APIs.

Default Parameter Values

```swift func log(_ message: String, level: String = "INFO") { print("[\(level)] \(message)") } log("Server started") // [INFO] Server started log("Disk full", level: "ERROR") // [ERROR] Disk full ``` Defaults must be placed after required parameters.

Multiple Defaults

```swift func connect(host: String, port: Int = 443, useTLS: Bool = true) {} connect(host: "api.example.com") connect(host: "api.example.com", port: 8080) connect(host: "api.example.com", port: 80, useTLS: false) ``` Callers can override any subset of defaulted parameters.

Variadic Parameters

A variadic parameter accepts zero or more values of the same type: ```swift func sum(_ numbers: Int...) -> Int { numbers.reduce(0, +) } print(sum(1, 2, 3)) // 6 print(sum(10, 20, 30, 40)) // 100 ``` Inside the function, `numbers` is an `[Int]` array.

Only One Variadic Per Function

Swift allows only one variadic parameter per function (it does not have to be last since Swift 5.4): ```swift func format(_ template: String, args: String...) -> String { var result = template for arg in args { result = result.replacingOccurrences(of: "%s", with: arg) } return result } ```

Combining Defaults and Variadics

```swift func build(separator: String = ", ", _ items: String...) -> String { items.joined(separator: separator) } build("apple", "banana", "cherry") // "apple, banana, cherry" build(separator: " | ", "one", "two", "three") // "one | two | three" ```

Passing an Array to a Variadic

You cannot pass an array directly to a variadic parameter: ```swift let nums = [1, 2, 3] // sum(nums) ❌ — won't compile ``` Unpack with `...` (Swift 5.4+ sequence spreading isn't built-in; use reduce/spread yourself): ```swift let total = nums.reduce(0, +) ```

Default Closures

Defaults work for closure parameters too: ```swift func retry(count: Int = 3, action: () -> Bool) {} retry { fetchData() } // count defaults to 3 retry(count: 5) { fetchData() } ``` Trailing-closure syntax still applies when using defaults.

Documenting Defaults

Comment or name defaults clearly so callers know what to expect: ```swift /// Sends a request. Timeout defaults to 30 seconds. func send(request: URLRequest, timeout: TimeInterval = 30.0) {} ``` Good documentation prevents callers from providing the default explicitly.

Real-World Pattern: print-like APIs

Swift's standard `print` is itself variadic with defaults: ```swift // Simplified signature: func print(_ items: Any..., separator: String = " ", terminator: String = "\n") print("a", "b", "c") // a b c print("x", "y", separator: "-") // x-y print("no newline", terminator: "") ```

Quick Check

Inside a function, what type does a variadic `Int...` parameter have?

Recap

Key takeaways: • Default values make parameters optional at the call site • Defaults must follow required parameters • Variadic parameters (`Type...`) accept zero or more values, become an array inside • Only one variadic per function • Combine both for flexible, clean APIs Next: in-out parameters and returning multiple values.

Frequently asked questions

Is the “Default and Variadic Parameters” lesson free?

Yes — the full text of “Default and Variadic Parameters” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.

What will I learn in “Default and Variadic Parameters”?

Setting default values and accepting variable numbers of arguments. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Swift Academy?

No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Default and Variadic Parameters” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Swift Academy lesson?

Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Argument Labels and Parameter Names
  2. Default and Variadic Parameters
  3. In-Out Parameters and Multiple Returns
  4. Functions as First-Class Values
← Back to Swift Academy