// Playground-noun: a place where people can play
import UIKit
// define and call functions
func sayHello (personName: String)-> String {
let greeting = "hello," + personName + "!"
return greeting
}
println (sayHello ("Anna"))
// method parameters and return values
// 1. multiple parameters
func halfOpenRangeLength (start: Int, endRange end: Int)-> Int {
return end-start
}
println (halfOpenRangeLength (1, endRange: 10))
// 2. no parameters
func sayHelloWorld ()-> String {
return "hello, world"
}
println (sayHelloWorld ())
// 3. no return value
func sayGoodbye (personName: String)-> Void {
println ("goodbye, \ (personName)!")
}
// func sayGoodbye (personName: String) {
// println ("goodbye, \ (personName)!")
//}
// func sayGoodbye (personName: String)-> () {
// println ("goodbye, \ (personName)!")
//}
sayGoodbye ("Anna")
// 4. multiple return values
func queryTupleReturnValue ()-> (first: Int, second: Int) {
return (1, 3)
}
let bounds = queryTupleReturnValue ()
println ("first is \ (bounds.first), second is \ (bounds.second)")
// 5. Return multiple optional values [whole optional, non-single element is optional]
func optionalTupleValue ()-> (first: Int, second: Int)? {
return (2, 4)
}
if let optionalTuple = optionalTupleValue () {
println ("optional: first is \ (optionalTuple.first), second is \ (optionalTuple.second)")
}
// method parameter name
// 1. External parameter name [The purpose is to more clearly see the role of each parameter when calling the method]
func join (string s1: String, toString s2: String, withJoiner joiner: String)-> String {
return s1 + joiner + s2
}
join (string: "hello", toString: "world", withJoiner: ",") // The meaning of each parameter is more clear
// 2. Mark the external parameter name and parameter name as consistent #
func containsCharacter (#string: String, #characterToFind: Character)-> Bool {
for character in string {
if character == characterToFind {
return true
}
}
return false
}
containsCharacter (string: "fsafh", characterToFind: "s")
// 3. Parameter default value [Specify a default value for the parameter, which can be ignored when calling the method]
func joinWithDefaultJoiner (string s1: String, toString s2: String, withJoiner joiner: String = "")-> String {
return s1 + joiner + s2
}
joinWithDefaultJoiner (string: "hello", toString: "world", withJoiner: ",")
joinWithDefaultJoiner (string: "hello", toString: "world")
// 4. External parameter name with default value parameter [In order to clearly see if a parameter with a default value already has a value provided by the caller when the method is called, swift will automatically generate an external parameter name for the friend's default value parameter. # Is added by default before the parameter name
func joinWithAutoExternalName (s1: String, s2: String, joiner: String = "")-> String {
return s1 + joiner + s2
}
joinWithAutoExternalName ("hello", "world", joiner: "-")
// 5. Deformable parameters [parameters are equivalent to array in the method, the type is determined by the type of the parameter]
func arithmeticMean (numbers: Double ...)-> Double {
var total: Double = 0
for number in numbers {
total + = number
}
return total / Double (numbers.count)
}
arithmeticMean (1, 3, 5, 8, 10) // numbers is equivalent to [1, 3, 5, 8, 10]
// Note: There can be at most one variable length parameter in a method, and it should be placed at the end of the parameter list
// 6. Constant and variable parameters [By default, the parameters in a method are constants (let) and cannot be changed inside the method. Using var to define variable parameters]
func modifyParameter (var string: String)-> String {
string + = "-modified" // Attempts to modify parameters using the default let will report an error
return string
}
modifyParameter ("hello")
// 7. Modify external variables inside the method
func modifyOutVar (inout string: String)-> String {
string + = "modified by func"
return string
}
var string = "to modify"
modifyOutVar (& string)
println ("modify: \ (string)")
// Note: 1. The in-out parameter cannot be set to a default value 2. The variable-length parameter cannot be an in-out parameter 3. The in-out parameter cannot use let or var 4. When the in-out parameter is passed, the address is passed
// method type
func addTwoInts (a: Int, b: Int)-> Int {
return a + b
}
// The above method type is (Int, Int)-> Int
func printHelloWorld () {
println ("hello, world")
}
// The above method type is ()-> () or ()-> Void
// 1. use method type
var mathFunction: (Int, Int)-> Int = addTwoInts
mathFunction (2, 3)
// 2. method type as a parameter
func printMathResult (mathFunction: (Int, Int)-> Int, a: Int, b: Int) {
let result = mathFunction (a, b)
println ("result: \ (result)")
}
printMathResult (mathFunction, 3, 13)
// 3. method type as return value
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
}
// transfer
var currentValue = 3
let moveNearerToZero = chooseStepFunction (currentValue> 0)
while currentValue! = 0 {
println ("currentvalue: \ (currentValue)")
currentValue = moveNearerToZero (currentValue)
}
println ("zero")
// 4. Nested functions
func anotherChooseStepFunction (backwards: Bool)-> (Int)-> Int {
func stepForward (input: Int)-> Int {
return input + 1
}
func stepBackward (input: Int)-> Int {
return input-1
}
return backwards? stepBackward: stepForward // use the same as before
}