Friday, April 8, 2016

closures and delegates

Spend a whole day reading closures and delegates.
I learned a lot but am still confused. Phew~


func differenceBetweenNumbers(a: Int, b:Int) -> (Int) {
  return a - b
}

// The type of “differenceBetweenNumbers" is (Int, Int) - > Int.

/*
 Now that we have a TYPE, we want to create a function that takes in
 this TYPE of function as an argument in its parameter. 
*/
func mathOperation(anyAddFunc: (Int, Int) -> Int, a: Int, b: Int) -> Int{
    return anyAddFunc(a,b)
}
// Calling the function

var someVariable = mathOperation(differenceBetweenNumbers, a: 10, b: 5)

// This will give you the result of 5


}

No comments:

Post a Comment