Bird
Raised Fist0
Swiftprogramming~10 mins

Shorthand argument names ($0, $1) in Swift - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Shorthand argument names ($0, $1)
Start closure call
↓
Use $0 for first argument
↓
Use $1 for second argument
↓
Perform operation with $0, $1
↓
Return result
↓
Closure ends
The closure starts, uses $0 and $1 as shorthand for its first and second arguments, performs operations, returns a result, and ends.
Execution Sample
Swift
let sum = { $0 + $1 }
let result = sum(3, 5)
print(result)
Defines a closure that adds two numbers using $0 and $1, then calls it with 3 and 5, printing the result.
Execution Table
StepActionEvaluationResult
1Define closure sumClosure expects two argumentsClosure stored in sum
2Call sum(3, 5)$0 = 3, $1 = 5Evaluate $0 + $1
3Calculate 3 + 53 + 58
4Print resultOutput result8
💡 Closure call completes and result 8 is printed
Variable Tracker
VariableStartAfter CallFinal
sumClosure (not called)Closure called with $0=3, $1=5Closure (unchanged)
resultundefined88
Key Moments - 2 Insights
Why do we use $0 and $1 instead of naming arguments?
Because the closure is short and Swift lets us use $0, $1 as quick names for the first and second arguments, as shown in execution_table step 2.
What happens if we use $2 in this closure?
There is no third argument, so using $2 would cause an error. The closure only has $0 and $1 as in execution_table step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of $1 when sum is called?
A3
B5
C8
Dundefined
💡 Hint
Check execution_table row 2 where $0=3 and $1=5
At which step is the closure result stored in variable 'result'?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at execution_table row 3 where calculation 3 + 5 equals 8
If we changed the closure to { $0 * $1 }, what would be printed?
A8
B35
C15
DError
💡 Hint
Multiply 3 * 5 as in variable_tracker after call values
Concept Snapshot
Shorthand argument names $0, $1 let you quickly access closure arguments without naming them.
Use $0 for the first argument, $1 for the second, and so on.
This is handy for short closures.
Example: let sum = { $0 + $1 } adds two numbers.
Call with sum(3, 5) returns 8.
Full Transcript
This visual trace shows how Swift shorthand argument names $0 and $1 work inside a closure. The closure sum is defined to add two numbers using $0 and $1 as quick argument names. When sum is called with 3 and 5, $0 becomes 3 and $1 becomes 5. The closure adds them to get 8, which is stored in result and printed. Beginners often wonder why $0 and $1 are used instead of named arguments; it's a shortcut for simple closures. Also, using $2 would cause an error because only two arguments exist. Changing the operation inside the closure changes the output accordingly.

Practice

(1/5)
1. What does the shorthand argument $0 represent in a Swift closure?
easy
A. The last argument passed to the closure
B. The first argument passed to the closure
C. The closure itself
D. The return value of the closure

Solution

  1. Step 1: Understand shorthand argument names

    In Swift closures, $0 refers to the first argument passed to the closure.
  2. Step 2: Identify $0 meaning

    Since $0 is always the first argument, it represents the first input value the closure receives.
  3. Final Answer:

    The first argument passed to the closure -> Option B
  4. Quick Check:

    $0 = first argument [OK]
Hint: Remember $0 is always the first input in closures [OK]
Common Mistakes:
  • Confusing $0 with the return value
  • Thinking $0 is the closure itself
  • Mixing $0 with the last argument
2. Which of the following is the correct way to use shorthand argument names to sort an array of integers in ascending order?
easy
A. let sorted = numbers.sorted { $1 > $0 }
B. let sorted = numbers.sorted { $1 < $0 }
C. let sorted = numbers.sorted { $0 > $1 }
D. let sorted = numbers.sorted { $0 < $1 }

Solution

  1. Step 1: Understand sorting with closures

    To sort ascending, the closure should return true if the first element is less than the second.
  2. Step 2: Match shorthand arguments to sorting logic

    Using $0 < $1 means the first element is less than the second, which sorts ascending.
  3. Final Answer:

    let sorted = numbers.sorted { $0 < $1 } -> Option D
  4. Quick Check:

    Ascending sort uses $0 < $1 [OK]
Hint: Use $0 < $1 for ascending sort closures [OK]
Common Mistakes:
  • Reversing $0 and $1 in comparison
  • Using > instead of < for ascending
  • Confusing sorting order logic
3. What is the output of the following Swift code?
let names = ["Anna", "Bob", "Charlie"]
let reversed = names.map { String($0.reversed()) }
print(reversed)
medium
A. ["annA", "boB", "eilrahc"]
B. ["Anna", "Bob", "Charlie"]
C. ["annA", "boB", "eilrahC"]
D. ["a n n A", "b o B", "c h a r l i e"]

Solution

  1. Step 1: Understand the map and reversed usage

    The map applies the closure to each name. $0 is each string. $0.reversed() reverses characters.
  2. Step 2: Convert reversed characters back to String

    Wrapping String($0.reversed()) creates a reversed string for each name.
  3. Final Answer:

    ["annA", "boB", "eilrahC"] -> Option C
  4. Quick Check:

    map + reversed string = reversed names [OK]
Hint: Remember map applies closure to each element using $0 [OK]
Common Mistakes:
  • Forgetting to convert reversed characters back to String
  • Expecting original array without changes
  • Confusing reversed string with spaced characters
4. Identify the error in this Swift code using shorthand argument names:
let numbers = [3, 1, 2]
let sorted = numbers.sorted { $2 < $1 }
print(sorted)
medium
A. Using $2 which does not exist in this closure
B. Missing return keyword in closure
C. Incorrect array declaration syntax
D. Using sorted instead of sort method

Solution

  1. Step 1: Check shorthand argument usage

    Shorthand arguments start at $0 for the first, $1 for the second. There is no $2 in a two-argument closure.
  2. Step 2: Identify invalid argument index

    Using $2 causes a compile error because only $0 and $1 exist here.
  3. Final Answer:

    Using $2 which does not exist in this closure -> Option A
  4. Quick Check:

    Only $0 and $1 allowed in two-argument closures [OK]
Hint: Use only $0 and $1 for two-parameter closures [OK]
Common Mistakes:
  • Assuming $2 exists in two-argument closures
  • Forgetting shorthand argument limits
  • Confusing sorted and sort method differences
5. Given the array let pairs = [(3, 5), (1, 2), (4, 4)], which closure using shorthand argument names correctly sorts the array by the sum of each tuple's elements in descending order?
hard
A. pairs.sorted { $0.0 + $0.1 > $1.0 + $1.1 }
B. pairs.sorted { $1.0 + $1.1 > $0.0 + $0.1 }
C. pairs.sorted { $0 + $1 > $1 + $0 }
D. pairs.sorted { $0.0 > $1.1 }

Solution

  1. Step 1: Understand tuple access and sorting logic

    Each element is a tuple. To sort by sum descending, compare sums of $0 and $1 tuples.
  2. Step 2: Check each option's correctness

    pairs.sorted { $0.0 + $0.1 > $1.0 + $1.1 } sums $0.0 + $0.1 and compares to $1.0 + $1.1 with > for descending order, which is correct.
  3. Final Answer:

    pairs.sorted { $0.0 + $0.1 > $1.0 + $1.1 } -> Option A
  4. Quick Check:

    Sum tuples and use > for descending sort [OK]
Hint: Sum tuple elements with $0.0 + $0.1 for sorting [OK]
Common Mistakes:
  • Mixing $0 and $1 order in comparison
  • Trying to add tuples directly without accessing elements
  • Using incorrect tuple element indices
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard