Recall & Review
beginner
What does using an underscore (_) before a function parameter in Swift do?
It tells Swift to omit the argument label when calling the function, so you only pass the value without naming the parameter.
Click to reveal answer
beginner
How do you define a Swift function that takes two integers without requiring argument labels when calling it?
Use underscores before both parameters like this:<br>
func add(_ a: Int, _ b: Int) -> Int { return a + b }Click to reveal answer
intermediate
Why might you want to omit argument labels in Swift functions?
To make function calls shorter and cleaner when the meaning is clear without labels, similar to how some built-in Swift functions work.
Click to reveal answer
beginner
What happens if you omit the underscore and call a Swift function without argument labels?
Swift will give a compile-time error because it expects argument labels by default unless you use an underscore to omit them.
Click to reveal answer
beginner
Example: How do you call this function?<br>
func greet(_ name: String) { print("Hello, \(name)!") }You call it without the argument label:<br>
greet("Alice")<br>Not like greet(name: "Alice").Click to reveal answer
What does the underscore (_) before a parameter name in a Swift function mean?
✗ Incorrect
Using _ before a parameter means you don't have to write the argument label when calling the function.
Given this function:<br>
func multiply(_ x: Int, y: Int) -> Int { x * y }<br>How do you call it correctly?✗ Incorrect
The first parameter omits the label, so just pass the value. The second requires the label 'y:'.
What error will you get if you call a function with argument labels omitted but the function requires them?
✗ Incorrect
Swift enforces argument labels at compile time unless you use _ to omit them.
Why might you NOT want to omit argument labels in Swift functions?
✗ Incorrect
Argument labels help make function calls clear and understandable.
How do you define a function with no argument labels for all parameters?
✗ Incorrect
Placing _ before each parameter name tells Swift to omit argument labels for those parameters.
Explain how to omit argument labels in Swift functions and why you might want to do it.
Think about how you write function calls without labels.
You got /4 concepts.
Describe what happens if you call a Swift function without argument labels when the function expects them.
Consider Swift's strictness about argument labels.
You got /3 concepts.