Skip to content

Commit a9e72dc

Browse files
committed
函数完毕
1 parent c468b4c commit a9e72dc

File tree

1 file changed

+166
-0
lines changed

1 file changed

+166
-0
lines changed

Control Flow.playground/Contents.swift

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,5 +148,171 @@ print(sayHi("Tim", true))
148148

149149

150150
//3:无返回值函数
151+
func sayGoodbye(personName: String) {
152+
print("Goodbye, \(personName)!")
153+
}
154+
sayGoodbye("Dave")
155+
156+
// 被调用时,一个函数的返回值可以被忽略
157+
//func printAndCount(StringToPrint:String) -> Int {
158+
// print(StringToPrint)
159+
// return StringToPrint.characters.count
160+
//}
161+
//func printWithoutCounting(stringToPrint:String) {
162+
// printAndCount(stringToPrint) // 被调用,函数的返回值被忽略
163+
//}
164+
//
165+
//printAndCount("hello world")
166+
167+
// 多重返回值函数
168+
// 使用元组类型让多个值作为一个复合值从函数中返回
169+
170+
//func minMax(array:[Int]) -> (min:Int,max:Int) {
171+
// var currentMin = array[0]
172+
// var currentMax = array[0]
173+
// for value in array[1..<array.count]{
174+
// if value < currentMin {
175+
// currentMin = value
176+
// }else if value > currentMax {
177+
// currentMax = value
178+
// }
179+
// }
180+
// return(currentMin,currentMax)
181+
//}
182+
//print(minMax([2,5,6,8,0,74,85]))
183+
184+
// 可选元组返回类型
185+
// 如果函数返回的元组类型有可能整个元组都没有值,则可以在元组的返回值的右括号中添加一个❓来表示该元组是一个可选的。
186+
187+
func minMax(array:[Int]) -> (min:Int,max:Int)? {
188+
var currentMin = array[0]
189+
var currentMax = array[0]
190+
for value in array[1..<array.count]{
191+
if value < currentMin {
192+
currentMin = value
193+
}else if value > currentMax {
194+
currentMax = value
195+
}
196+
}
197+
return(currentMin,currentMax)
198+
}
199+
print(minMax([2,5,6,8,0,74,85]))
200+
// 可以使用可选绑定来检查minMax:函数返回的是一个实际的元组值还是nil
201+
202+
203+
// 函数参数名称
204+
205+
// 函数参数都有有一个外部参数名和一个局部参数名,外部参数名用于在函数调用时标注传递给函数的参数,局部参数名在函数的实现内部使用
206+
//func someFunction(firstParameterName:Int,secondParameterName:Int) {
207+
//
208+
//}
209+
//
210+
//someFunction(1, 2)
211+
212+
// 指定外部参数名:在局部参数名前指定外部参数名,中间以空格分隔
213+
func say(to person:String,and anotherPerson:String) -> String {
214+
return "hello \(person) and \(anotherPerson)"
215+
}
216+
print(say(to: "Bill", and: "Ted"))
217+
218+
// 可变参数:一个可变参数可以接受零个或多个值。函数调用时, 你可以用可变参数来指定函数参数可以被传入不确定数量的输入值通过在变量名后面加入(...)的方式来定义可变参数。
219+
220+
// 可变参数的传入值在函数体中变为此类型的一个数组。一个函数最多只有一个可变参数
221+
func arithmeticMean(numbers:Double...) -> Double {
222+
var total:Double = 0
223+
for number in numbers {
224+
total += number
225+
}
226+
return total / Double(numbers.count)
227+
}
228+
arithmeticMean(1,2,3,4,5)
229+
230+
231+
// 常量参数和变量参数
232+
//func alignRight(var String:String,totalLength:Int,pad:Character) -> String {
233+
// let amountToPad = totalLength - String.characters.count
234+
// if amountToPad<1{
235+
// return String
236+
// }
237+
// let padstring = String(pad)
238+
// for _ in 1...amoutToPad {
239+
// String = padstring + String
240+
// }
241+
// return String
242+
//}
243+
//
244+
//let originalString = "hello"
245+
//let paddedString = alignRight(originalString, 10, "-")
246+
247+
// 输入输出参数
248+
// 如果你想要一个函数可以修改参数的值,并且想要在这些修改在函数调用结束后仍然存在,那么就应该把这个参数定义为输入输出参数.在参数定义前加input关键字,传入参数时,需要在参数名前加&符,表示这个值可以被修改
249+
func swapTwoInts(inout a:Int,inout b:Int){
250+
let temporaryA = a
251+
a = b
252+
b = temporaryA
253+
}
254+
var someInt = 3
255+
var anotherInt = 107
256+
swapTwoInts(&someInt, &anotherInt)
257+
258+
259+
// 函数类型
260+
func addTwoInts(a:Int,b:Int) -> Int {
261+
return a + b
262+
}
263+
264+
func mulitiplyTwoInts(a:Int,b:Int) -> Int {
265+
return a * b
266+
}
267+
268+
// 使用函数类型
269+
// 在swift中,使用函数类型就想使用其他类型一样。可以定义一个类型为函数的常量或变量,并将适当的函数赋值给它
270+
var mathFunction:(Int,Int) -> Int = addTwoInts
271+
// 通过使用mathFunction来调用被赋值的函数
272+
print("\(mathFunction(2,3))")
273+
274+
// 有相同匹配类型的不同函数可以被赋值给同一个变量
275+
mathFunction = mulitiplyTwoInts
276+
print("\(mathFunction(2,3))")
277+
278+
// 函数类型作为参数类型
279+
func printMathResult(mathFunction:(Int,Int) -> Int,a
280+
:Int,b:Int) -> Int {
281+
// print("\(mathFunction(a,b))")
282+
return mathFunction(a,b)
283+
}
284+
printMathResult(addTwoInts, 3, 4)
285+
286+
// 函数类型作为返回类型
287+
// 在返回箭头后写一个完整的函数类型
288+
289+
func stepForward(input:Int) -> Int {
290+
return input + 1
291+
}
292+
293+
func stepBackward(input:Int) -> Int {
294+
return input - 1
295+
}
296+
297+
func chooseStepFunction(backwards:Bool) -> (Int) ->Int {
298+
return backwards ? stepBackward : stepForward
299+
}
300+
var currentValue = 3
301+
let moveNearerToZero = chooseStepFunction(currentValue > 0)
302+
303+
// 嵌套函数 : 在一个函数体中定义另一个函数,叫做嵌套函数
304+
// 默认情况下,嵌套函数是对外界不可见的,但是可以被它们的外围函数调用,一个外围函数也可以返回它的某一个嵌套函数,使得这个函数可以在其他领域中被调用
305+
306+
307+
308+
309+
310+
311+
312+
313+
314+
315+
316+
151317

152318

0 commit comments

Comments
 (0)