Using _ to Omit Argument Labels in Swift Functions
📖 Scenario: Imagine you are creating a simple calculator app that adds two numbers. You want to make the function call easy and clean by omitting argument labels.
🎯 Goal: Build a Swift function that adds two numbers without requiring argument labels when calling it.
📋 What You'll Learn
Create a function named add that takes two Int parameters without argument labels.
Use _ to omit argument labels in the function definition.
Call the function with two numbers without labels.
Print the result of the addition.
💡 Why This Matters
🌍 Real World
Omitting argument labels makes function calls simpler and more natural, especially in small utility functions or when the meaning is clear from context.
💼 Career
Swift developers often use <code>_</code> to improve code readability and API design in iOS app development.
Progress0 / 4 steps
1
Create the add function with two parameters
Write a function named add that takes two Int parameters named a and b. For now, just return 0 inside the function.
Swift
Hint
Define the function with func add(a: Int, b: Int) -> Int and return 0 for now.
2
Omit argument labels using _
Modify the add function so that both parameters a and b omit argument labels by using _ before their names.
Swift
Hint
Use _ before each parameter name like func add(_ a: Int, _ b: Int) -> Int.
3
Implement addition logic inside the function
Change the add function to return the sum of a and b.
Swift
Hint
Return a + b inside the function.
4
Call the function without argument labels and print the result
Call the add function with arguments 5 and 7 without labels, store the result in sum, and print sum.
Swift
Hint
Call add(5, 7), assign to sum, then print sum.
Practice
(1/5)
1. What does the underscore _ do when used before a function parameter in Swift?
easy
A. It changes the parameter type to Any.
B. It makes the parameter optional.
C. It makes the parameter a constant inside the function.
D. It allows you to call the function without writing the argument label.
Solution
Step 1: Understand the role of underscore in parameters
The underscore before a parameter name tells Swift to omit the argument label when calling the function.
Step 2: Effect on function call
This means you can pass the argument directly without naming it, making the call shorter and simpler.
Final Answer:
It allows you to call the function without writing the argument label. -> Option D
Quick Check:
Underscore = omit argument label [OK]
Hint: Underscore means skip argument label when calling [OK]
Common Mistakes:
Thinking underscore makes parameter optional
Confusing underscore with type changes
Assuming underscore affects parameter mutability
2. Which of the following function declarations correctly omits the argument label for the first parameter?
easy
A. func greet(_name: String) { print("Hello, \(_name)!") }
B. func greet(_ name: String) { print("Hello, \(name)!") }
C. func greet(name _ : String) { print("Hello, \(name)!") }
D. func greet(name: String) { print("Hello, \(name)!") }
Solution
Step 1: Identify correct underscore usage
The underscore must appear before the parameter name with a space, like _ name: String.
A. Cannot mix omitted and required labels in parameters
B. Missing argument label for denominator
C. No error, code runs fine
D. Missing argument label for numerator
Solution
Step 1: Analyze parameter labels
The first parameter uses underscore, so no label needed. The second requires label denominator.
Step 2: Check function call
The call uses divide(10, denominator: 2), which matches the parameter labels correctly.
Final Answer:
No error, code runs fine -> Option C
Quick Check:
Underscore omits first label, second label required [OK]
Hint: First param no label, second param label required [OK]
Common Mistakes:
Expecting both labels omitted
Trying to omit second label incorrectly
Confusing parameter order
5. You want to write a Swift function formatDate that takes a Date and a String format. You want to call it like formatDate(myDate, "yyyy-MM-dd") without argument labels. Which function declaration is correct?