Swift Academy · Lesson

Defaults in Function Parameters

Give parameters default values to simplify call sites.

Lesson 3 of 413 steps

Defaults in Function Parameters is a free Swift Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 4 lessons in total.

What Are Default Parameters

A function parameter can have a default value. Callers may omit that argument, and Swift fills in the default automatically.

func greet(name: String = "Guest") {
    print("Hello, \(name)")
}
greet()
greet(name: "Ada")

Declaring a Default

You assign a default after the parameter type using =. The default is used only when the caller does not pass that argument.

func power(base: Int, exponent: Int = 2) -> Int {
    var result = 1
    for _ in 0..<exponent { result *= base }
    return result
}
print(power(base: 5))
print(power(base: 5, exponent: 3))

Multiple Defaults

You can give several parameters defaults. Callers override only the ones they care about.

func makeBox(width: Int = 1, height: Int = 1) -> Int {
    return width * height
}
print(makeBox())
print(makeBox(width: 4))

Mixing Required and Default

Required parameters have no default; default parameters do. Place required ones first so callers can omit trailing defaults cleanly.

func send(message: String, urgent: Bool = false) {
    print("\(urgent ? "URGENT: " : "")\(message)")
}
send(message: "Hi")
send(message: "Fire!", urgent: true)

Defaults Reduce Overloads

Before default parameters, you might write several overloaded functions. One function with defaults replaces them all.

func connect(host: String, port: Int = 80) {
    print("\(host):\(port)")
}
connect(host: "example.com")
connect(host: "example.com", port: 8080)

Override Any Subset

You can override one default while keeping others, as long as you use argument labels to be explicit.

func style(size: Int = 12, bold: Bool = false, italic: Bool = false) {
    print("\(size) bold:\(bold) italic:\(italic)")
}
style(bold: true)

Defaults Can Be Expressions

A default value can be any expression of the right type, evaluated when the argument is omitted.

func now(stamp: Int = 1000 + 1) {
    print(stamp)
}
now()
now(stamp: 5)

Default of an Optional

A common pattern is an optional parameter defaulting to nil, letting callers skip it entirely.

func log(_ text: String, tag: String? = nil) {
    print("[\(tag ?? "general")] \(text)")
}
log("Started")
log("Saved", tag: "db")

Combining With Nil Coalescing

Default parameters and ?? work well together: accept an optional, then coalesce inside the body.

func price(amount: Int? = nil) -> Int {
    return amount ?? 10
}
print(price())
print(price(amount: 25))

Defaults in Initializers

The same feature works in struct initializers, letting you create objects with sensible defaults.

struct Settings {
    var volume: Int
    init(volume: Int = 50) {
        self.volume = volume
    }
}
print(Settings().volume)
print(Settings(volume: 80).volume)

Readability Benefit

Default parameters make call sites shorter and communicate the common case, while still allowing full control when needed.

func repeatText(_ text: String, times: Int = 1) {
    for _ in 0..<times { print(text) }
}
repeatText("Hi")
repeatText("Yo", times: 2)

Quick Check

Test default parameters.

Recap: Defaults in Function Parameters

Default parameter values let callers omit arguments. Declare them with = after the type, mix them with required parameters, and pair them with ?? or optionals for flexible APIs.

func tax(amount: Int, rate: Int = 10) -> Int {
    return amount + amount * rate / 100
}
print(tax(amount: 100))
Free to start

Learn Swift with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
122
Lessons
409

Frequently Asked Questions

Is the “Defaults in Function Parameters” lesson free?

Yes — any 3 lessons of the Swift Academy learning path, including “Defaults in Function Parameters”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 4 lessons in total.

What will I learn in “Defaults in Function Parameters”?

Give parameters default values to simplify call sites. 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, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.

How long does the “Defaults in Function 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. The Nil-Coalescing Operator
  2. Chaining Default Values
  3. Defaults in Function Parameters
  4. Dictionary Default Subscripts
← Back to Swift Academy