Ans : A callback function is a function that is passed as an argument to another function, to be “called back” at a later time.
Code for Example :
Here each function type is ( [Int], (Int, Int) -> Void ) .
Here callback function type is ( (Int, Int) -> Void ) .
Callback function mostly used when we want to pass block of code that should be executed in another function execution complete.
Code for Example :
func printValue(index: Int, element: Int) {
print("index = \(index), Element = \(element)")
}
func each(array: [Int], callback: (Int, Int) -> Void) {
for i in 0..<array.count {
callback(i, array[i])
}
}
each(array: [1, 2, 3, 4, 5],callback: printValue)
Here each function type is ( [Int], (Int, Int) -> Void ) .
Here callback function type is ( (Int, Int) -> Void ) .
Callback function mostly used when we want to pass block of code that should be executed in another function execution complete.
No comments:
Post a Comment
Thanks