Default parameter values in Swift - Time & Space Complexity
We want to see how using default values for function parameters affects how long the code takes to run.
Does adding default values change how the program grows with bigger inputs?
Analyze the time complexity of the following code snippet.
func greet(times: Int = 1) {
for _ in 1...times {
print("Hello!")
}
}
greet(times: 3)
This function prints "Hello!" a number of times, using a default value if no number is given.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that prints "Hello!" multiple times.
- How many times: It runs exactly as many times as the input number (times).
Each time you increase the input number, the loop runs more times, printing more messages.
| Input Size (times) | Approx. Operations (prints) |
|---|---|
| 10 | 10 |
| 100 | 100 |
| 1000 | 1000 |
Pattern observation: The number of prints grows directly with the input number.
Time Complexity: O(n)
This means the time it takes grows in a straight line with the input number.
[X] Wrong: "Using default values makes the function faster or slower depending on the default."
[OK] Correct: The default value only sets what happens if no input is given; it does not change how many times the loop runs when a number is provided.
Understanding how default parameters work helps you explain function behavior clearly and shows you can think about how code runs as inputs change.
"What if the function called itself recursively instead of using a loop? How would the time complexity change?"
Practice
default parameter values in Swift functions?Solution
Step 1: Understand default parameters
Default parameter values allow a function to use a preset value if no argument is provided.Step 2: Identify the purpose
This makes calling the function easier and avoids errors from missing arguments.Final Answer:
To provide fallback values when arguments are not passed -> Option BQuick Check:
Default parameters = fallback values [OK]
- Thinking default values speed up code
- Believing default values force arguments
- Confusing default parameters with variable declarations
Solution
Step 1: Review Swift default parameter syntax
In Swift, default values are assigned using an equals sign after the parameter type.Step 2: Check each option
func greet(name: String = "Guest") { print("Hello, \(name)!") } uses correct syntax:name: String = "Guest". Others have syntax errors.Final Answer:
func greet(name: String = "Guest") { print("Hello, \(name)!") } -> Option DQuick Check:
Default parameter syntax = type = value [OK]
- Placing default value outside parentheses
- Using = after parameter name without type
- Trying to assign default inside function body
func multiply(_ a: Int, by b: Int = 2) -> Int {
return a * b
}
print(multiply(5))Solution
Step 1: Understand function parameters
The function multiply takes two parameters: 'a' and 'b' with a default value of 2.Step 2: Evaluate the call multiply(5)
Only 'a' is provided as 5, so 'b' uses default 2. Result is 5 * 2 = 10.Final Answer:
10 -> Option CQuick Check:
5 * default 2 = 10 [OK]
- Expecting error for missing argument
- Using first argument for both parameters
- Confusing default value with multiplication factor
func greet(name: String = "User", _ age: Int) {
print("Hello \(name), you are \(age) years old.")
}Solution
Step 1: Check parameter order rules
In Swift, parameters with default values must come after parameters without defaults.Step 2: Analyze given function
'name' has a default but appears before 'age' which has no default. This causes a compile error.Final Answer:
Default parameter must be last in the parameter list -> Option AQuick Check:
Default params last = correct order [OK]
- Putting default parameters before non-default ones
- Assuming return type is mandatory for void functions
- Confusing type restrictions on default values
formatMessage that takes a text and an optional prefix with default value "Info:". It should return a string combining prefix and text separated by a space. Which is the correct function and call to get output "Info: Hello"?Solution
Step 1: Define function with default parameter last
Parameter with default 'prefix' must come after 'text' without default.Step 2: Check function call and output
Calling with only 'text' uses default prefix "Info:" and returns "Info: Hello".Final Answer:
func formatMessage(text: String, prefix: String = "Info:") -> String { return "\(prefix) \(text)" } print(formatMessage(text: "Hello")) -> Option AQuick Check:
Default param last + call with one arg = correct output [OK]
- Placing default parameter before non-default
- Calling function without required parameters
- Swapping order of prefix and text in output
Ready to prove it?
Pass the test and this pattern moves unknown
