📖 Scenario: You are creating a simple greeting app that can say hello to users. Sometimes, users provide their name, and sometimes they don't. You want the app to greet users properly in both cases.
🎯 Goal: Build a Swift function that uses default parameter values to greet users by name or with a generic greeting if no name is given.
📋 What You'll Learn
Create a function called greet that takes one parameter name of type String.
Set a default value for the name parameter to "Guest".
Call the greet function twice: once without any argument and once with the argument "Alice".
Print the greeting message inside the greet function.
💡 Why This Matters
🌍 Real World
Default parameter values are useful in apps where some inputs are optional, like greeting users who may or may not provide their names.
💼 Career
Understanding default parameters helps you write cleaner, more flexible functions, a skill important for Swift developers building user-friendly apps.
Progress0 / 4 steps
1
Create the greet function with a name parameter
Write a Swift function called greet that takes one parameter named name of type String. Inside the function, print the message "Hello, \(name)!".
Swift
Hint
Define a function with func, add a parameter name: String, and use print with string interpolation.
2
Add a default value to the name parameter
Modify the greet function so that the name parameter has a default value of "Guest".
Swift
Hint
Add = "Guest" after the parameter type to set a default value.
3
Call greet without an argument
Call the greet function without passing any argument.
Swift
Hint
Just write greet() to call the function with the default parameter.
4
Call greet with the argument "Alice" and print both greetings
Call the greet function with the argument "Alice" after the call without arguments. The program should print both greetings.
Swift
Hint
Call greet("Alice") after greet() to see both greetings.
Practice
(1/5)
1. What is the main purpose of using default parameter values in Swift functions?
easy
A. To make functions run faster
B. To provide fallback values when arguments are not passed
C. To force the caller to always provide arguments
D. To declare variables inside the function
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 B
Quick Check:
Default parameters = fallback values [OK]
Hint: Default values fill in missing arguments automatically [OK]
Common Mistakes:
Thinking default values speed up code
Believing default values force arguments
Confusing default parameters with variable declarations
2. Which of the following is the correct way to declare a Swift function with a default parameter value?
easy
A. func greet(name: String) { name = "Guest"; print("Hello, \(name)!") }
B. func greet(name: String) = "Guest" { print("Hello, \(name)!") }
C. func greet(name: String default "Guest") { print("Hello, \(name)!") }
D. func greet(name: String = "Guest") { print("Hello, \(name)!") }
Solution
Step 1: Review Swift default parameter syntax
In Swift, default values are assigned using an equals sign after the parameter type.
func multiply(_ a: Int, by b: Int = 2) -> Int {
return a * b
}
print(multiply(5))
medium
A. Error: Missing argument for parameter 'b'
B. 5
C. 10
D. 25
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 C
Quick Check:
5 * default 2 = 10 [OK]
Hint: If argument missing, default value is used [OK]
Common Mistakes:
Expecting error for missing argument
Using first argument for both parameters
Confusing default value with multiplication factor
4. Identify the error in this Swift function declaration:
func greet(name: String = "User", _ age: Int) {
print("Hello \(name), you are \(age) years old.")
}
medium
A. Default parameter must be last in the parameter list
B. Missing return type
C. Cannot use default values with String type
D. Function body syntax error
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 A
Quick Check:
Default params last = correct order [OK]
Hint: Place parameters with defaults after others [OK]
Common Mistakes:
Putting default parameters before non-default ones
Assuming return type is mandatory for void functions
Confusing type restrictions on default values
5. You want to write a Swift function 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"?