How to pass a runnable procedure (a function) as a parameter and run it in Swift

1 Answer

0 votes
import Foundation

// Define a function that accepts a closure (function) as a parameter
func runProcedure(task: @escaping () -> Void) {
    task() // Execute the passed function
}

// Define a function to pass as a parameter
func myTask() {
    print("Executing my task!")
}

// Pass the function to the procedure
runProcedure(task: myTask)



/*
run:

Executing my task!

*/

 



answered May 22, 2025 by avibootz
...