9.13 Functions as Parameters
An interesting feature of functions within Swift is that they can be treated as data types. It is perfectly valid, for example, to assign a function to a constant or variable as illustrated in the declaration below:
func inchesToFeet (_ inches: Float) -> Float {
return inches * 0.0833333
}
let toFeet = inchesToFeet
The above code declares a new function named inchesToFeet and subsequently assigns that function to a constant named toFeet. Having made this assignment, a call to the function may be made using the constant name instead of the original function name:
let result = toFeet(10)
On the surface this does not seem to be a particularly compelling feature. Since we could already call the function without assigning it to a constant or variable data type it does not seem that much has been gained.
The possibilities that this feature offers become more apparent when we consider that a function...