Swift Learning Notes

Functions

2021-07-30  本文已影响0人  宋奕Ekis

Function Parameters and Return Values

Functions With an Implicit Return

We can ignore the return keyword when the entire body of function is a single expression, it can return this expression implicitly.

func greeting(for person: String) -> String {
    "Hello, " + person + "!"
}
print(greeting(for: "Dave"))
// Prints "Hello, Dave!"

Function Argument Labels and Parameter Names

Specifying Argument Labels

In swift, the function can have argument label and parameter name both, the argument label is used for external calling, and the parameter name is used for internal implementation.

We can define the argument label before the parameter name.

func greet(person: String, from hometown: String) -> String {
    return "Hello \(person)!  Glad you could visit from \(hometown)."
}
print(greet(person: "Bill", from: "Cupertino"))

I love this characteristic!

Variadic Parameters

func arithmeticMean(_ numbers: Double...) -> Double {
    var total: Double = 0
    for number in numbers {
        total += number
    }
    return total / Double(numbers.count)
}
arithmeticMean(1, 2, 3, 4, 5)
// returns 3.0, which is the arithmetic mean of these five numbers
arithmeticMean(3, 8.25, 18.75)
// returns 10.0, which is the arithmetic mean of these three numbers

In-Out Parameters

Normally, we can change the value which once we transfer from outside in a function, but we can use this keyword inout to define an mutable value in function parameters.

func swapTwoInts(_ a: inout Int, _ b: inout Int) {
    let temporaryA = a
    a = b
    b = temporaryA
}

var someInt = 3
var anotherInt = 107
swapTwoInts(&someInt, &anotherInt)
print("someInt is now \(someInt), and anotherInt is now \(anotherInt)")
// Prints "someInt is now 107, and anotherInt is now 3"

Function Types

The function type means that we can define a type to describe a function, which made up of the parameter types and the return types.

For example:

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

In the above code, we can get one function type – – (Int, Int) -> Int, addTwoInts and multiplyTwoInts have same type!

So we can implement below instance:

var mathFunction: (Int, Int) -> Int = addTwoInts
print("Result: \(mathFunction(2, 3))")
mathFunction = multiplyTwoInts
print("Result: \(mathFunction(2, 3))")
// Prints "Result: 6"

That means we can also use the function as a parameters of another function.

func printMathResult(_ mathFunction: (Int, Int) -> Int, _ a: Int, _ b: Int) {
    print("Result: \(mathFunction(a, b))")
}
printMathResult(addTwoInts, 3, 5)

And as a reture value!

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

Beautiful!

Let’s think!

上一篇 下一篇

猜你喜欢

热点阅读