Functions are first-class in Swift—named parameters, default values, variadic args, and in-out parameters make APIs readable, similar to Kotlin but with external parameter labels by default.
Syntax and labels
func greet(name: String, excited: Bool = false) -> String {
let suffix = excited ? "!" : "."
return "Hello, \(name)\(suffix)"
}
print(greet(name: "Ada"))
print(greet(name: "Ada", excited: true))
External labels appear at call sites; omit with _ when the first argument label would be redundant.
Returning multiple values
Tuples and named return elements avoid temporary wrapper types: func minMax(_ nums: [Int]) -> (min: Int, max: Int).
Important interview questions and answers
- Q: Why parameter labels?
A: Call-site clarity at API boundaries—move(from:to:)reads like English. - Q: Functions as values?
A: Yes—store in variables, pass to higher-order functions, capture in closures.
Self-check
- How do you omit an external parameter label?
- Can functions return tuples?
Tip: Omit external labels with _ when the first argument label would read awkwardly at call sites.
Interview prep
- Parameter labels?
External labels at call sites for readable APIs—omit with _.
- Return tuples?
Yes—named tuple elements avoid wrapper types.