Introduction
Swift functions offer flexible ways to define parameters and return values beyond a single required input and a single output. You can give parameters default values, accept a variable number of arguments with variadic parameters, pass values by reference using in-out parameters, and return multiple values at once using tuples. These features let you design cleaner, more flexible function signatures.
Cricket analogy: A bowler's function signature can default to a standard field placement unless the captain overrides it, accept a variadic number of overs bowled in a spell, use an inout parameter to let a runner directly adjust the scoreboard, and return a tuple of both runs conceded and wickets taken at once.
Syntax
func greet(name: String = "World") -> String { ... }
func sum(_ numbers: Int...) -> Int { ... }
func swapValues(_ a: inout Int, _ b: inout Int) { ... }
func minMax(of numbers: [Int]) -> (min: Int, max: Int) { ... }Explanation
A default parameter value lets callers omit that argument entirely, in which case Swift substitutes the default. A variadic parameter, written with three dots after the type (e.g. Int...), accepts zero or more values of that type, which are made available inside the function as an array. An in-out parameter, marked with inout, lets a function modify a caller's variable directly rather than working on a copy; callers must pass an & before the argument to indicate it may be mutated. Finally, a tuple return type lets a single function return multiple named values at once, avoiding the need for a custom struct just to bundle a couple of related results.
Cricket analogy: Omitting the overrideField argument lets Swift substitute the default field setting; wickets... gathers a variadic number of dismissals into an array inside the function; an inout runsScored parameter marked with & lets an over directly update the caller's scoreboard; and a tuple return hands back (runs, wickets) together without a custom struct.
Example
func greet(name: String = "World") -> String {
return "Hello, \(name)!"
}
func sum(_ numbers: Int...) -> Int {
return numbers.reduce(0, +)
}
func swapValues(_ a: inout Int, _ b: inout Int) {
let temp = a
a = b
b = temp
}
func minMax(of numbers: [Int]) -> (min: Int, max: Int) {
return (numbers.min()!, numbers.max()!)
}
print(greet())
print(sum(1, 2, 3, 4))
var x = 5
var y = 10
swapValues(&x, &y)
print(x, y)
let result = minMax(of: [3, 7, 1, 9])
print(result.min, result.max)Output
Hello, World!
10
10 5
1 9Key Takeaways
- Default parameter values let callers omit arguments, e.g.
name: String = "World". - Variadic parameters (
Int...) accept a variable number of values as an array inside the function. inoutparameters allow a function to modify the caller's variable directly, requiring&at the call site.- Tuples let a function return multiple named values without defining a custom type.
- These features make function signatures more flexible and expressive.
Practice what you learned
1. How do you give a parameter a default value in Swift?
2. What syntax declares a variadic parameter?
3. What must callers write before an argument passed to an inout parameter?
4. What does a function that returns `(min: Int, max: Int)` allow?
5. Inside a function, how is a variadic parameter like `numbers: Int...` accessed?
Was this page helpful?
You May Also Like
Functions in Swift
Learn how to declare, call, and label the parameters of reusable blocks of code called functions in Swift.
Closures in Swift
Understand closures as self-contained blocks of functionality that capture and store references to surrounding variables.
Optionals in Swift
Optionals let a Swift variable represent either a value or the absence of one, forming the basis of Swift's null-safety.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics