What if your functions could guess missing information and still work perfectly?
Why Default parameter values in Swift? - Purpose & Use Cases
Imagine you are writing a function to greet people. Sometimes you want to say "Hello, John!" and other times just "Hello!" without a name. Without default values, you must write two separate functions or always provide a name, even if you don't have one.
This manual way is slow and clumsy. You repeat code or force users to give extra information. It's easy to make mistakes or forget to handle cases where no name is given. This makes your code messy and harder to maintain.
Default parameter values let you set a standard value for a function's input. If no value is given, the function uses the default automatically. This keeps your code clean, simple, and flexible without extra effort.
func greet(name: String) {
print("Hello, \(name)!")
}
// Must always call greet(name: "John")func greet(name: String = "") { if name.isEmpty { print("Hello!") } else { print("Hello, \(name)!") } } // Can call greet() or greet(name: "John")
You can write simpler functions that work well in many situations without extra code or confusion.
Think about a coffee order app where the user can choose a size but if they don't, the app automatically picks a medium size. Default values make this easy and smooth.
Default values reduce repeated code and errors.
They make functions easier to use and more flexible.
They help keep your code clean and simple.
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
