Function types

Unlike OC, Swift defines a type for each function, consisting of a parameter type and a return value type. For example, the following two functions:

func addTwoInts(_ a: Int, _ b: Int) -> Int {
    return a + b
}
func multiplyTwoInts(_ a: Int, _ b: Int) -> Int {
    return a * b
}
Copy the code

This example defines two functions that take two int values and return one int value. These two functions are defined as (Int, Int) -> Int, which can be interpreted as: a function takes two parameters, both of Int, and returns a value of Int. The same applies to functions that have no arguments or return values:

Func printHelloWorld () {print(" hello, world ")}Copy the code

It is of type ()-> Void.

The use of function types

The function in the above example can be assigned to a variable in Swift:

var mathFunction: (Int, Int) -> Int = addTwoInts
Copy the code

Define a variable named mathFunction whose type is a function that takes two Int values and returns an Int. Set this new variable to refer to a function named addTwoInts. , so that you can call mathFunction directly is the same as calling addTwoInts.

Function as argument

Functions as arguments are like OC blocks. In Swift, you can use the function type (Int, Int) -> Int as the argument type of another function, leaving the implementation to the function caller. Examples are as follows:

func printMathResult(_ mathFunction: (Int, Int) -> Int, _ a: Int, _ b: Int) {
    print("Result: \(mathFunction(a, b))")
}
printMathResult(addTwoInts, 3, 5)
// Prints "Result: 8"
Copy the code

Function as the return value

You can add a function type as the return type of another function by following the return arrow (->) of a function with a return value. The following two functions:

func stepForward(_ input: Int) -> Int {
    return input + 1
}
func stepBackward(_ input: Int) -> Int {
    return input - 1
}
Copy the code

Their function types are (Int) -> Int, and define a chooseStepFunction(backward:) so that its return value type is also (Int) -> Int:

func chooseStepFunction(backward: Bool) -> (Int) -> Int {
    return backward ? stepBackward : stepForward
}
Copy the code

ChooseStepFunction is actually called to return a stepBackward or stepForward function based on backward:

var currentValue = 3
let moveNearerToZero = chooseStepFunction(backward: currentValue > 0)
print(moveNearerToZero(currentValue))
Copy the code

Nested function

Redefining a function within a function is called a nested function. Functions inside functions are theoretically hidden from the outside, but they can still be called externally. Nested functions can also be returned from within a function to be used in another scope. For example in the above example, you can write stepForward and stepBackward inside chooseStepFunction as nested functions:

func chooseStepFunction(backward: Bool) -> (Int) -> Int {
    func stepForward(input: Int) -> Int { return input + 1 }
    func stepBackward(input: Int) -> Int { return input - 1 }
    return backward ? stepBackward : stepForward
}
Copy the code

The call is the same as above.

Reference:Functions