Welcome to FutureAppLaboratory

v=(*^ワ^*)=v

Learning Swift Part 6 - Functions

| Comments

Just some notes after reading Apple’s official online guide. Most code are from the guide, with some modification by me.

Tuple in switch statement is interesting.

===== Full Notes =====

Functions

  • Defining and Calling Functions
1
2
3
4
    func sayHello(personName: String) -> String {
        let greeting = "Hello, " + personName + "!"
        return greeting
    }
  • Function Parameters and Return Values
  • Multiple Input Parameters
1
2
3
4
5
    func halfOpenRangeLength(start: Int, end: Int) -> Int {
        return end - start
    }
    println(halfOpenRangeLength(1, 10))
    // prints "9"
  • Functions Without Parameters
1
2
3
4
5
    func sayHelloWorld() -> String {
        return "hello, world"
    }
    println(sayHelloWorld())
    // prints "hello, world"
  • Function Without Return Values
1
2
3
4
5
    func sayGoodbye(personName: String) {
        println("Goodbye, \(personName)!")
    }
    sayGoodbye("Dave")
    // prints "Goodbye, Dave!"
  • Functions with Multiple Return Values
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    func minMax(array: [Int]) -> (min: Int, max: Int) {
        var currentMin = array[0]
        var currentMax = array[0]
        for value in array[1..<array.count] {
            if value < currentMin {
                currentMin = value
            } else if value > currentMax {
                currentMax = value
            }
        }
        return (currentMin, currentMax)
    }

    let bounds = minMax([8, -6, 2, 109, 3, 71])
    println("min is \(bounds.min) and max is \(bounds.max)")
    // prints "min is -6 and max is 109"
  • Optional Tuple Return Types
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    func minMax(array: [Int]) -> (min: Int, max: Int)? {
        if array.isEmpty { return nil }
        var currentMin = array[0]
        var currentMax = array[0]
        for value in array[1..<array.count] {
            if value < currentMin {
                currentMin = value
            } else if value > currentMax {
                currentMax = value
            }
        }
        return (currentMin, currentMax)
    }

    if let bounds = minMax([8, -6, 2, 109, 3, 71]) {
        println("min is \(bounds.min) and max is \(bounds.max)")
    }
    // prints "min is -6 and max is 109"
  • External parameter Names
1
2
3
4
5
6
7
8
9
10
11
12
    func someFunction(externalParameterName localParameterName: Int) {
        // function body goes here, and can use localParameterName
        // to refer to the argument value for that parameter
    }

    func join(string s1: String, toString s2: String, withJoiner joiner: String)
        -> String {
            return s1 + joiner + s2
    }

    join(string: "hello", toString: "world", withJoiner: ", ")
    // returns "hello, world"
  • Shorthand External Parameter Names
1
2
3
4
5
6
7
8
9
10
11
    func containsCharacter(#string: String, #characterToFind: Character) -> Bool {
        for character in string {
            if character == characterToFind {
                return true
            }
        }
        return false
    }

    let containsAVee = containsCharacter(string: "aardvark", characterToFind: "v")
    // containsAVee equals true, because "aardvark" contains a "v"
  • Default Parameter Values
1
2
3
4
5
6
7
8
9
10
    func join(string s1: String, toString s2: String,
        withJoiner joiner: String = " ") -> String {
            return s1 + joiner + s2
    }

    join(string: "hello", toString: "world", withJoiner: "-")
    // returns "hello-world"

    join(string: "hello", toString: "world")
    // returns "hello world"
  • External Names for Parameters with Default Values (automatically generated)
1
2
3
4
5
6
    func join(s1: String, s2: String, joiner: String = " ") -> String {
        return s1 + joiner + s2
    }

    join("hello", "world", joiner: "-")
    // returns "hello-world"
  • Variadic Parameters (varargs)
1
2
3
4
5
6
7
8
9
10
11
12
13
    // Note that Double... is different from [Double]

    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
  • Constant and Variable Parameters
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    func alignRight(var string: String, count: Int, pad: Character) -> String {
        let amountToPad = count - countElements(string)
        if amountToPad < 1 {
            return string
        }
        let padString = String(pad)
        for _ in 1...amountToPad {
            string = padString + string
        }
        return string
    }
    let originalString = "hello"
    let paddedString = alignRight(originalString, 10, "-")
    // paddedString is equal to "-----hello"
    // originalString is still equal to "hello"
  • In-Out Parameters
1
2
3
4
5
6
7
8
9
10
11
12
13
    // Variable parameters, as described above, can only be changed within the function itself. If you want a function to modify a parameter’s value, and you want those changes to persist after the function call has ended, define that parameter as an in-out parameter instead.

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

    var someInt = 3
    var anotherInt = 107
    swapTwoInts(&someInt, &anotherInt)
    println("someInt is now \(someInt), and anotherInt is now \(anotherInt)")
    // prints "someInt is now 107, and anotherInt is now 3"
  • Function Type
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    func addTwoInts(a: Int, b: Int) -> Int {
        return a + b
    }
    func multiplyTwoInts(a: Int, b: Int) -> Int {
        return a * b
    }

    var mathFunction: (Int, Int) -> Int = addTwoInts

    println("Result: \(mathFunction(2, 3))")
    // prints "Result: 5"

    mathFunction = multiplyTwoInts
    println("Result: \(mathFunction(2, 3))")
    // prints "Result: 6"
  • Function Types as Parameter Types
1
2
3
4
5
    func printMathResult(mathFunction: (Int, Int) -> Int, a: Int, b: Int) {
        println("Result: \(mathFunction(a, b))")
    }
    printMathResult(addTwoInts, 3, 5)
    // prints "Result: 8"
  • Function Type as Return Types
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
    func stepForward(input: Int) -> Int {
        return input + 1
    }
    func stepBackward(input: Int) -> Int {
        return input - 1
    }

    func chooseStepFunction(backwards: Bool) -> (Int) -> Int {
        return backwards ? stepBackward : stepForward
    }

    var currentValue = 3
    let moveNearerToZero = chooseStepFunction(currentValue > 0)
    // moveNearerToZero now refers to the stepBackward() function

    println("Counting to zero:")
    // Counting to zero:
    while currentValue != 0 {
        println("\(currentValue)... ")
        currentValue = moveNearerToZero(currentValue)
    }
    println("zero!")
    // 3...
    // 2...
    // 1...
    // zero!
  • Nested Functions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    func chooseStepFunction(backwards: Bool) -> (Int) -> Int {
        func stepForward(input: Int) -> Int { return input + 1 }
        func stepBackward(input: Int) -> Int { return input - 1 }
        return backwards ? stepBackward : stepForward
    }
    var currentValue = -4
    let moveNearerToZero = chooseStepFunction(currentValue > 0)
    // moveNearerToZero now refers to the nested stepForward() function
    while currentValue != 0 {
        println("\(currentValue)... ")
        currentValue = moveNearerToZero(currentValue)
    }
    println("zero!")
    // -4...
    // -3...
    // -2...
    // -1...
    // zero!

Comments