函数Function
1.无参数无返回值
- funcprintHelloWorld() {
- print("hello,world") @H_502_31@}
2.有参数无返回值
- funcsayGoodbye(personName:String) {
- print("Goodbye,\(personName)!") @H_502_31@}
- sayGoodbye("Dave")
- // prints "Goodbye,Dave!"
3.一个参数
- funcsayHello(personName:String) -> String{
- letgreeting ="Hello," +personName +"!"
- returngreeting @H_502_31@}
- print(sayHello("Anna"))
- // prints "Hello,Anna!"
- print(sayHello("Brian"))
函数名 参数名 参数类型 返回值类型
func sayHello(personName:String) -> String
4.多个参数
- funcsayHello(personName:String,alreadyGreeted: Bool) -> String{
- ifalreadyGreeted {
- returnsayHelloAgain(personName) @H_502_31@ } else {
- returnsayHello(personName) @H_502_31@ } @H_502_31@}
- print(sayHello("Tim",alreadyGreeted:true))
- // prints "Hello again,Tim!"
上面的在函数的调用过程中,参数里面可以带参数名称的: alreadyGreeted:true
多参数之间使用”,”分开.
5.多值返回
-
@H_502_31@funcminMax(array: [Int]) -> (min:Int,max: Int) {
- varcurrentMin =array[0]
- varcurrentMax =array[0]
- forvalueinarray[1..<array.count] { @H_502_31@ if value < currentMin { @H_502_31@ currentMin = value @H_502_31@ } else if value >currentMax { @H_502_31@ currentMax = value @H_502_31@ } @H_502_31@ }
- return (currentMin,currentMax) @H_502_31@}
-
@H_502_31@letbounds =minMax([8,-6,2,109,3,71])
- print("min is \(bounds.min) and max is\(bounds.max)")
- // prints "min is -6 and max is 109"
6.可选元组返回
-
@H_502_31@funcminMax(array: [Int]) -> (min:Int,max: Int)? {
@H_502_31@ if array.isEmpty {returnnil }
- ) @H_502_31@}
-
@H_502_31@ifletbounds =minMax([8,71]) {
- print("min is\(bounds.min) and max is\(bounds.max)") @H_502_31@}
- // prints "min is -6 and max is 109"
和上面的多值返回相比 在函数{}前多了一个”?”.
7.指定外部参数名称
- funcsayHello(toperson:String,and anotherPerson:String) ->String{
- return"Hello\(person) and\(anotherPerson)!" @H_502_31@}
- print(sayHello(to:"Bill",and: "Ted"))
- // prints "Hello Bill and Ted!"
8.省去外部参数名
- funcsomeFunction(firstParameterName:Int,_ secondParameterName:Int) {
- // function body goes here
- // firstParameterName and secondParameterName refer to
- // the argument values for the first and second parameters
- print(“result is\(firstParameterName + secondParameterName)") @H_502_31@}
- someFunction(1,2)
// prints “result is 3”
9.带初始参数
- funcsomeFunction(parameterWithDefault:Int = 12) {
- // if no arguments are passed to the function call,
- // value of parameterWithDefault is 12
- returnparameterWithDefault
@H_502_31@}
- someFunction(6)// parameterWithDefault is 6
- someFunction()// parameterWithDefault is 12
10.不定参数个数
- funcarithmeticMean(numbers:Double...) -> Double { @H_502_31@ var total: Double = 0
- fornumberinnumbers { @H_502_31@ total += number @H_502_31@ }
- returntotal /Double(numbers.count) @H_502_31@}
- 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
11.常亮和变量参数
- funcalignRight(varstring:String,totalLength:Int,pad:Character) -> String{
- letamountToPad =totalLength -string.characters.count @H_502_31@ if amountToPad < 1 { @H_502_31@ return string @H_502_31@ }
- letpadString =String(pad) @H_502_31@ for _ in1...amountToPad {
- string =padString +string @H_502_31@ } @H_502_31@ return string @H_502_31@}
- letoriginalString ="hello"
- letpaddedString =alignRight(originalString,totalLength:10,pad:"-")
- // paddedString is equal to "-----hello"
- // originalString is still equal to "hello"
12.In-Out 参数
- func swapTwoInts(inouta:Int,inout _ b: Int) {
- lettemporaryA =a @H_502_31@ a = b
- b =temporaryA @H_502_31@}
- varsomeInt =3
- varanotherInt =107
- swapTwoInts(&someInt,&anotherInt)
- print("someInt is now\(someInt),and anotherInt is now\(anotherInt)")
- // prints "someInt is now 107,and anotherInt is now 3"
in-out参数不能有初始默认值,inout参数不能是变量.上面的函数swapTwoInts
没有返回参数但是也完成了相似的功能.
13.函数赋值函数
varmathFunction: (Int,Int ) ->Int= addTwoInts
- print("Result: \(mathFunction(2,3))")
- // prints "Result: 5"
14.函数作为参数
- funcprintMathResult(mathFunction: (Int, Int ) -> Int,_a:Int ,_b: Int) {
- print("Result:\(mathFunction(a,b))") @H_502_31@}
- printMathResult(addTwoInts,17)"> // prints "Result: 8"
15.函数作为返回值
- funcstepForward(input:Int) -> Int{ @H_502_31@ return input + 1 @H_502_31@}
- funcstepBackward(input:Int) -> Int{ @H_502_31@ return input - 1 @H_502_31@}
- funcchooseStepFunction(backwards:Bool) -> ( Int) -> Int{
- returnbackwards ?stepBackward :stepForward @H_502_31@}
- varcurrentValue =3
- letmoveNearerToZero =chooseStepFunction(currentValue >0)
- // moveNearerToZero now refers to the stepBackward() function
原文地址: