欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【Swift4】(5) 函数基本使用 | 可变参数 | inout引用传递 | 函数类型返回值 | 函数嵌套

程序员文章站 2024-03-17 16:18:04
...

函数基本使用

关键字:func
func hello(name:String) ->String
{
    let result =  "Hello,"+name
    return result
}
hello(name: "imagine")

可选型:

func hello(name:String?,greet:String) ->String
{
    let result =  greet+","+(name)!
    return result
}

var nickname:String?  //nil
nickname = "imagine"
hello(name: nickname,greet: "Good Night") //Good Night,imagine"

无参数函数,直接返回一个字符串类型的函数:

func sayHello() ->String
{
    return "Welcome to imaginecode"
}
sayHello() //"Welcome to imaginecode"

空类型void / () ,不返回任何值

func sayVoid() ->Void{
    print("it is a void func")
}

使用元组让函数返回多个值

func maxminScores( scores:[Int]) -> ( maxscore:Int,minscore:Int)? //元组的可选型
{
    if scores.isEmpty{
        return nil
    }
    var curmax = scores[0] ,curmin = scores[0]
    for score in scores[1..<scores.count]{
        curmax = max(curmax, score)
        curmin = min(curmin, score)
    }
    return (curmax,curmin)
}
var scores:[Int]? = [12,60,71,81,91,100] //可选型数组
scores = scores ?? []
if let result = maxminScores(scores: scores!)
{
    print(result.maxscore)
    print(result.minscore)
}

内部参数名和外部参数名

func hello(userName nickname:String,greeting greet:String) -> String{ //内部参数nickname,greet,属于函数体
    let result = greet+":"+nickname
    return result
}
hello(userName: "imagine", greeting: "codeing") //给参数nickname与greet起了外部参数名userName和greeting

参数的默认值

func hello(nickname:String,greet:String = "hello") -> String{ //给greet默认值hello
    let result = greet+":"+nickname
    return result
}
hello(nickname: "imagine") //"hello:imagine"
func hello(nickname:String,greet:String = "hello",other:String = "nihao") -> String{ //给greet默认值hello
    let result = greet+":"+nickname+other
    return result
}
hello(nickname: "imagine",other:"how do you do") //"hello:imaginehow do you do"

可变参数

  • 一个函数最好只能设置一个可变参数,并且该可变参数只能放在这个函数参数列表的最后一个位置
  • 必须参数 > 默认值参数 > 可变参数
func add(a:Int,b:Int,others:Int ... ) ->Int //others是可变参数 ... 将其解析为数组
{
    var result = a + b
    for num in others
    {
        result += num
    }
    return result
}
var res = add(a: 2, b: 3)

res = add(a: 2, b: 3, others: 4,5,6)

NSLog(format: String, args: CVarArg...) //CvarArg也是可变参数

inout参数 - 引用传递

  • inout用于声明数据是地址传递,也称之为引用传递;
  • inout修饰的参数是不能有默认值的,有范围的参数集合也不能被修饰;
  • 一个参数一旦被inout修饰,就不能再被var和let修饰了。
func swapTwoInts( a:inout Int,b:inout Int)
{
    let t = a;
    a = b
    b = t
}
var x = 0, y = 100

swapTwoInts(a: &x, b: &y) //传入引用参数

函数类型

func add(a:Int,b:Int) -> Int
{
    return a+b
}

let anotherAdd:(Int,Int)->Int = add //参数为两个Int,返回类型为Int  ,add 作为变量


anotherAdd(3,4)
func changeScores1(scores:inout [Int]) {
    for i in 0..<scores.count {
        scores[i] = Int(sqrt(Double(scores[i]))*10)
    }
}

func changeScores2(scores:inout [Int]) {
    for i in 0..<scores.count {
        scores[i] = Int(sqrt(Double(scores[i]))/150.0 * 100.0)
    }
}
func changeScores3(scores:inout [Int]) {
    for i in 0..<scores.count {
        scores[i] += 3
    }
}

var scores1 = [20,40,60,80,90]
changeScores1(scores: &scores1)

var scores2 = [20,40,60,80,90]
changeScores2(scores: &scores2)

var scores3 = [20,40,60,80,90]
changeScores3(scores: &scores3)

改进:

func changeScores(op:(Int)->Int, scores:inout [Int])
{
    for i  in 0..<scores.count{
        scores[i] = op(scores[i])
    }
}
func op1(x:Int)->Int {return Int(sqrt(Double(x))*10)}
func op2(x:Int)->Int {return Int(sqrt(Double(x))/150.0*100.0)}
func op3(x:Int)->Int {return x + 3}

var scores1 = [20,40,60,80,90]
changeScores(op: op1, scores: &scores1)

var scores2 = [20,40,60,80,90]
changeScores(op: op2, scores: &scores2)

var scores3 = [20,40,60,80,90]
changeScores(op: op3, scores: &scores3)

var arr = [Int]()

for _ in 1...20
{
    arr.append(Int(arc4random()%100))
}
arr

func compareTwoInts(a:Int,b:Int) -> Bool{return a>b }

arr.sort()

返回函数类型的返回值、函数嵌套

//邮费计算
func mailcost1(weight:Int) -> Int
{
    return 1*weight
}
func mailcost2(weight:Int) -> Int
{
    return 2*weight
}
func chooseMailCostMethod(weight:Int) -> (Int)->Int //返回一个Int类型的函数,解耦作用
{
    return weight <= 10 ? mailcost1 : mailcost2
}

func totalCost(price:Int,weight:Int) -> Int
{
    let mailCost:(Int)->Int = chooseMailCostMethod(weight: weight)
    return mailCost(weight) + price*weight
}

另一种写法:函数嵌套

func mailcost1(weight:Int) -> Int
{
    return 1*weight
}
func mailcost2(weight:Int) -> Int
{
    return 2*weight
}

func totalCost(price:Int,weight:Int) -> Int
{
    func chooseMailCostMethod(weight:Int) -> (Int)->Int //函数嵌套
    {
        return weight <= 10 ? mailcost1 : mailcost2
    }
    
    let mailCost:(Int)->Int = chooseMailCostMethod(weight: weight)
    return mailCost(weight) + price*weight
}