0
0
Swiftprogramming~10 mins

Trailing closure syntax in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Trailing closure syntax
Call function with closure
Is closure last argument?
NoUse normal closure syntax
Yes
Use trailing closure syntax
Execute closure body
Return from function
This flow shows how Swift decides to use trailing closure syntax when the closure is the last argument in a function call.
Execution Sample
Swift
func greet(action: () -> Void) {
    print("Start")
    action()
    print("End")
}

greet {
    print("Hello from closure")
}
This code calls a function with a trailing closure that prints a message between two prints.
Execution Table
StepActionOutputNotes
1Call greet with trailing closureStartFunction starts, prints 'Start'
2Execute closure bodyHello from closureClosure prints its message
3Continue greet functionEndFunction prints 'End'
4Function returnsExecution ends
💡 Function completes after printing 'End' and returning
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
actionclosure passedclosure readyclosure executedclosure donefunction done
Key Moments - 2 Insights
Why can we omit parentheses when using trailing closure syntax?
Because the closure is the last argument, Swift allows omitting parentheses and writing the closure outside the parentheses, as shown in step 1 of the execution_table.
What happens if the closure is not the last argument?
Trailing closure syntax cannot be used; the closure must be inside the parentheses, as explained in the concept_flow decision step.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is printed at Step 2?
A"Start"
B"End"
C"Hello from closure"
DNothing
💡 Hint
Check the Output column for Step 2 in the execution_table
At which step does the function print "End"?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the Output column in the execution_table for when "End" is printed
If the closure was not the last argument, what would change in the code?
AWe must write the closure inside parentheses
BWe could still use trailing closure syntax
CThe closure would be ignored
DThe function would not compile
💡 Hint
Refer to the concept_flow decision about closure position
Concept Snapshot
Trailing closure syntax lets you write a closure outside the parentheses if it is the last argument.
Syntax: functionName { closureBody }
Use it to make code cleaner and more readable.
If closure is not last, write it inside parentheses.
Swift automatically associates the trailing closure with the last parameter.
Full Transcript
Trailing closure syntax in Swift allows you to write a closure argument outside the parentheses when it is the last argument in a function call. The example shows a function greet that takes a closure. When calling greet, the closure is written after the parentheses, making the code cleaner. The execution steps show the function printing "Start", then the closure prints "Hello from closure", and finally the function prints "End" before returning. This syntax is only allowed if the closure is the last argument. If not, the closure must be inside the parentheses.