0
0
Swiftprogramming~10 mins

Closures as function parameters in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Closures as function parameters
Define function with closure parameter
Call function, pass closure
Function executes closure
Closure runs, returns result
Function uses closure result
End
This flow shows how a function takes a closure as a parameter, calls it, and uses its result.
Execution Sample
Swift
func greetUser(action: () -> String) {
    let message = action()
    print(message)
}

greetUser { "Hello, Swift!" }
This code defines a function that takes a closure returning a string, calls it, and prints the message.
Execution Table
StepActionEvaluationResult
1Define function greetUser with closure parameterNo execution yetFunction ready to be called
2Call greetUser with closure { "Hello, Swift!" }Closure passed as argumentFunction starts execution
3Inside greetUser, call closure action()Closure runs"Hello, Swift!"
4Assign closure result to messagemessage = "Hello, Swift!"message holds "Hello, Swift!"
5Print messageprint("Hello, Swift!")Output: Hello, Swift!
6Function greetUser endsNo more codeExecution complete
💡 Function ends after printing the closure's returned string.
Variable Tracker
VariableStartAfter Step 3After Step 4Final
messageundefined"Hello, Swift!""Hello, Swift!""Hello, Swift!"
Key Moments - 3 Insights
Why do we call action() inside the function?
Because action is a closure passed in, calling action() runs the closure and gets its returned value, as shown in step 3 of the execution_table.
What type of value does the closure return?
The closure returns a String, which is assigned to the variable message in step 4 of the execution_table.
Can the closure be written outside the function call?
Yes, but here it is passed inline for simplicity, as shown in step 2 of the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of message after step 4?
A"Hello, Swift!"
Bundefined
CClosure itself
Dnil
💡 Hint
Check the variable_tracker row for message after step 4.
At which step does the closure actually run?
AStep 4
BStep 2
CStep 3
DStep 5
💡 Hint
Look at the execution_table action and evaluation columns for when action() is called.
If the closure returned "Hi!" instead, what would be printed?
A"Hello, Swift!"
B"Hi!"
CNothing
DError
💡 Hint
The printed output depends on the closure's return value shown in step 5.
Concept Snapshot
func functionName(parameter: () -> ReturnType) {
  let result = parameter()
  // use result
}

Call with closure:
functionName { returnValue }

The closure runs inside the function when called as parameter().
Full Transcript
This example shows how a Swift function can take a closure as a parameter. The function greetUser takes a closure named action that returns a String. When greetUser is called, it runs the closure by calling action(), stores the returned string in message, and prints it. The closure is passed inline as { "Hello, Swift!" }. The execution steps show defining the function, calling it with the closure, running the closure, assigning its result, printing, and ending. The variable message starts undefined and holds the closure's returned string after step 3. Key points include understanding that calling action() runs the closure and returns its value, which the function uses. The quiz checks understanding of when the closure runs, what message holds, and how changing the closure affects output.