Last active
February 27, 2019 12:20
-
-
Save cyyeh/04e9b969b7cfe23e2481a82c4e37a697 to your computer and use it in GitHub Desktop.
Swift Functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| Referenced from Stanford CS193p | |
| #: important | |
| ##: very important | |
| Functions | |
| - Defining and Calling Functions | |
| - Function Parameters and Return Values | |
| - Functions Without Parameters | |
| - Functions With Multiple Parameters | |
| - Functions Without Return Values | |
| - Functions With Multiple Return Values | |
| - #Function Argument Labels and Parameter Names | |
| - #Specifying Argument Labels | |
| - Omitting Argument Labels | |
| - Default Parameter Values | |
| - ##Function Types | |
| - Nested Functions | |
| Reference: https://docs.swift.org/swift-book/LanguageGuide/Functions.html | |
| */ | |
| /* | |
| Defining and Calling Functions | |
| */ | |
| func greet(person: String) -> String { | |
| let greeting = "Hello, " + person + "!" | |
| return greeting | |
| } | |
| print(greet(person: "Anna")) | |
| // Prints "Hello, Anna!" | |
| print(greet(person: "Brian")) | |
| // Prints "Hello, Brian!" | |
| func greetAgain(person: String) -> String { | |
| return "Hello again, " + person + "!" | |
| } | |
| print(greetAgain(person: "Anna")) | |
| // Prints "Hello again, Anna!" | |
| /* | |
| Function Parameters and Return Values | |
| */ | |
| // Functions Without Parameters | |
| func sayHelloWorld() -> String { | |
| return "hello, world" | |
| } | |
| print(sayHelloWorld()) | |
| // Prints "hello, world" | |
| // Functions With Multiple Parameters | |
| func greet(person: String, alreadyGreeted: Bool) -> String { | |
| if alreadyGreeted { | |
| return greetAgain(person: person) | |
| } else { | |
| return greet(person: person) | |
| } | |
| } | |
| print(greet(person: "Tim", alreadyGreeted: true)) | |
| // Prints "Hello again, Tim!" | |
| // Functions Without Return Values | |
| func anotherGreet(person: String) { | |
| print("Hello, \(person)!") | |
| } | |
| anotherGreet(person: "Dave") | |
| // Prints "Hello, Dave!" | |
| // Functions With Multiple Return Values | |
| 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(array: [8, -6, 2, 109, 3, 71]) | |
| print("min is \(bounds.min) and max is \(bounds.max)") | |
| // Prints "min is -6 and max is 109" | |
| /* | |
| #Function Argument Labels and Parameter Names | |
| */ | |
| func someFunction(firstParameterName: Int, secondParameterName: Int) { | |
| // In the function body, firstParameterName and secondParameterName | |
| // refer to the argument values for the first and second parameters. | |
| } | |
| someFunction(firstParameterName: 1, secondParameterName: 2) | |
| // #Specifying Argument Labels | |
| func someFunction(argumentLabel parameterName: Int) { | |
| // In the function body, parameterName refers to the argument value | |
| // for that parameter. | |
| } | |
| func greet(person: String, from hometown: String) -> String { | |
| return "Hello \(person)! Glad you could visit from \(hometown)." | |
| } | |
| print(greet(person: "Bill", from: "Cupertino")) | |
| // Prints "Hello Bill! Glad you could visit from Cupertino." | |
| /* | |
| Omitting Argument Labels | |
| */ | |
| func someFunction(_ firstParameterName: Int, secondParameterName: Int) { | |
| // In the function body, firstParameterName and secondParameterName | |
| // refer to the argument values for the first and second parameters. | |
| } | |
| someFunction(1, secondParameterName: 2) | |
| // Default Parameter Values | |
| func someFunction(parameterWithoutDefault: Int, parameterWithDefault: Int = 12) { | |
| // If you omit the second argument when calling this function, then | |
| // the value of parameterWithDefault is 12 inside the function body. | |
| } | |
| someFunction(parameterWithoutDefault: 3, parameterWithDefault: 6) // parameterWithDefault is 6 | |
| someFunction(parameterWithoutDefault: 4) // parameterWithDefault is 12 | |
| /* | |
| ##Function Types | |
| */ | |
| func addTwoInts(_ a: Int, _ b: Int) -> Int { | |
| return a + b | |
| } | |
| func multiplyTwoInts(_ a: Int, _ b: Int) -> Int { | |
| return a * b | |
| } | |
| // Using Function Types | |
| var mathFunction: (Int, Int) -> Int = addTwoInts | |
| print("Result: \(mathFunction(2, 3))") | |
| // Prints "Result: 5" | |
| mathFunction = multiplyTwoInts | |
| print("Result: \(mathFunction(2, 3))") | |
| // Prints "Result: 6" | |
| // Function Types as Parameter Types | |
| func printMathResult(_ mathFunction: (Int, Int) -> Int, _ a: Int, _ b: Int) { | |
| print("Result: \(mathFunction(a, b))") | |
| } | |
| printMathResult(addTwoInts, 3, 5) | |
| // Prints "Result: 8" | |
| // Function Types as Return Types | |
| 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 | |
| } | |
| var currentValue = 3 | |
| var moveNearerToZero = chooseStepFunction(backward: currentValue > 0) | |
| // moveNearerToZero now refers to the stepBackward() function | |
| print("Counting to zero:") | |
| // Counting to zero: | |
| while currentValue != 0 { | |
| print("\(currentValue)... ") | |
| currentValue = moveNearerToZero(currentValue) | |
| } | |
| print("zero!") | |
| /* | |
| Nested Functions | |
| */ | |
| func chooseAnotherStepFunction(backward: Bool) -> (Int) -> Int { | |
| func stepForward(input: Int) -> Int { return input + 1 } | |
| func stepBackward(input: Int) -> Int { return input - 1 } | |
| return backward ? stepBackward : stepForward | |
| } | |
| currentValue = -4 | |
| moveNearerToZero = chooseAnotherStepFunction(backward: currentValue > 0) | |
| // moveNearerToZero now refers to the nested stepForward() function | |
| while currentValue != 0 { | |
| print("\(currentValue)... ") | |
| currentValue = moveNearerToZero(currentValue) | |
| } | |
| print("zero!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment