初学Swift_函数

/* 函数

 函数的定义和调用


 */

/**

 打招呼函数

 

 - parameter personName: 姓名(参数)

 

 - returns: 打招呼语言(返回值)

 */

func sayHello(personName:String) -> String {

    let greeting = "hello" + personName + "!"

    

    return greeting;

}

//函数的调用

print(sayHello("zhang"));


//多参数函数

func halfOpenRangeLength(strar:Int , end:Int) -> Int {

    return end - strar

}

print(halfOpenRangeLength(1, end: 10))

//无参数函数

func sayHelloWorld() -> String {

    return "hello world !"

}

print(sayHelloWorld())

//无返回值得函数


func sayGoodbye(personName:String){

   print("Goodbye \(personName)")

}

sayGoodbye("Li")

//多重返回值函数

func count(hh:String) -> (vowels: Int, consonants:Int, others:Int) {

    var vowels = 0, consonants = 0,others = 0

    for character in hh.characters{

        switch String(character) {

        case "o","t","a","b":

            vowels += 1

        case"h":

                others += 1;

        default:

            consonants += 1

        }

    }

    return(vowels, consonants, others)

}

//调用

let total = count("wo shi yi ge da hao ren hahahahahha")

print(total.consonants)

/**

 参数名称

 - parameter parameterName: 局部参数(只能在函数体中使用, 不能再函数调用的时候使用)

 */

func someFunction(parameterName: Int){

}

/**

 外部参数名

 //注意:如果提供了外部参数名 那么在函数被调用的时候 必须使用外部参数名

 //作用明确每个参数的含义

 - parameter localParameterName: 外部参数(外部参数名写在局部参数名之前 用空格' '分开)

 */

func someFunctionTo(externalParameterName localParameterName: Int ){

    print(localParameterName)

}


someFunctionTo(externalParameterName: 2)


func sayHelloBody(firstName liname:String, andName helloName:String, secName:String) -> String {

    return liname + secName + helloName

}

//调用

sayHelloBody(firstName: "wo", andName: "hello", secName: "ni")

/**

 默认参数值

 - parameter s1:     参数1

 - parameter s2:     参数2

 - parameter joiner: 默认参数值

 - returns: 返回值

 */

func joinDefault(string s1:String, toString s2:String, withJoiner joiner:String = "hehe") -> String {

    return s1 + joiner + s2

}

joinDefault(string: "haNI", toString: "wuwu");

func joinDe(S1:String , joiner:String = "") -> String {

    return S1 + joiner

}

/**

 可变参数

 注意:一个函数至多能有一个可变的参数 而且他必须是参数表中的最后一个这样为了避免函数调用时候出现歧义

 - parameter numbers: 可变参数

 

 - returns: 返回值

 */

func arithmeticMean(numbers:Double...) -> Double {

    var total:Double = 0

    for num in numbers {

      total += num

    }

    return total

}

arithmeticMean(1,2,3,3)

/**

 变量参数(输入输出参数):var形式被去除

 可以对参数进行更改(可以写)

 - parameter string:参数1

 - parameter totalLength:参数2

 - parameter pad:参数3

 

 - returns: 返回值

 */

func alignRight(inout string:String, totalLength: Int, pad:Character) -> String {

    let amount = totalLength - 5

    if amount < 1 {

        return string

    }

    let padString = String(pad)

    for _ in 1...amount {

        string = padString + string

    }

    return string

}

 /*

inout: 可以改变一个参数的值并且在函数调用结束后修改后的值仍然存在,

:传入函数的值,这个值被函数修改,然后被传出函数,替换原来的值。

 如果将变量作为inout 就不可以传入常量或者字面量,因为这些亮是不可以被修改的

 当传入inout参数时 需要在参数前加&, 表示这个值是可以被修改的

 如果用inout标记参数,这个参数不能被var 或者let标记

*/

var hh = "hhhhhheeeehhheee"

alignRight(&hh, totalLength: 7, pad: "f")


func swap(inout a:Int, inout b:Int){

    let temp = a

    a = b

    b = temp

}

var red = 3

var blue = 4

swap(&red, &blue)

red

blue

/* 函数的类型

 函数的类型是由函数的参数类型和返回值类型组成

 */

/**

 该函数的函数类型为:

 (Int, Int) -> Int

 - parameter a: 参数1

 - parameter b: 参数2

 - returns: 返回值

 */

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

/**

 *  调用

 */

print("result:\(mathFunction(2,3))")

/**

 *  相同匹配类型的不同函数

 */

mathFunction = multiplyTwoInts

print("result:\(mathFunction(2,3))")


/**

  函数类型作为参数类型

 - parameter mathFunction: 参数1: 函数

 - parameter a:            参数2

 - parameter b:            参数3

 */

func printMathResult(mathFunction:(Int,Int) -> Int, a:Int, b:Int ) {

    print("result: \(mathFunction(a,b))")

}

/**

 *  调用

 */

printMathResult(mathFunction, a: 2, b: 3)

/**

 函数类型作为返回参数

*/


func stepForward(input:Int) -> Int {

    print(input);

    return input + 1

}

func stepBackward(input:Int) -> Int {

    print(input);

    return input - 1

}

/**

 返回值是函数的函数

 - parameter Backwards: 参数

 

 - returns: 返回值是一个参数是Int型返回值是Int型的函数

 */

func chooseStepFunction(Backwards:Bool) -> (Int) -> Int {

    

    return Backwards ? stepBackward: stepForward

}


var currentValue = 3

let moveNearerToZero = chooseStepFunction(currentValue > 0)

while currentValue != 0{

    currentValue = moveNearerToZero(currentValue)

    print("\(currentValue)")

}

func h(b:Double) -> Bool {

    if b > 0 {

        return false

    }else{

        return true

    }

}


func boolFunction(a:Int) -> (Double) ->Bool {

    return h

}


/**

 调用

 */

print(boolFunction(4)(4))

/**

 *  嵌套函数

 */


func chooseStepFunction1(backwards: Bool) -> (Int) ->Int {

    

    func stepForward(input: Int) -> Int{return input +  1}

    func stepBackward(input: Int) -> Int {return input - 1}

    return backwards ? stepForward: stepBackward;

}


var current = -4

let moveNearToZero = chooseStepFunction1(current < 0);

while current != 0{

    current = moveNearToZero(current)

    print("\(current)")

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值