Swift:(六)、函数

//函数:

//1)、函数的定义和调用

//定义:func+空格+方法名(参数名:参数类型)-> 返回值类型

func sayhello(person:String) -> String{

    let hicontent = "hello " + person

    return hicontent

}

//调用:下面传入的参数称为实参

print(sayhello(person: "huangjingzheng"))


//简化:

func sayhello1(person:String) -> String{

    return "hello " + person

}


//(2)、函数的参数和返回值

//a、无参数函数 (无参有返回值) 无参函数函数名后不可以省略()

func sayHelloWorld()->String{

    return "Hello World!"

}

sayHelloWorld()


//b、多参函数

func sayhello(person:String,canSay:Bool)->String{

    if canSay {

        return "hello " + person

    }else{

        return person

    }

}


//函数名一样,参数不一样,可以重复定义该函数名的函数

sayhello(person: "doudou", canSay: true)



func sayhello(age:Int,canSay:Bool)->String{

    if canSay {

        return "hello " + String(age)

    }else{

        return String(age)

    }

}


sayhello(age: 23, canSay: true)


//c、无返回值的函数:没有返回值的时候,函数依然返回一个返回值viod的特殊值,实际上这个viod是一个空的元组,没有元素,可写成()

func sayhello(person:String){

    print("Hello \(person)")

}

sayhello(person: "jiadongliang")



//d、函数被调用时返回值可以忽略

func strCont(string:String)->Int{

    print(string)

    return string.characters.count

}

strCont(string: "Hello world!")//输出一个字符串并返回一个Int 型的字符数


func printWithCont(string:String){

    let _ = strCont(string: string)

}

printWithCont(string: "Hello world!")//调用第一个函数,忽略返回值,只输出一个字符串


//e、多返回值:Int数组中返回最小值喝最大值

func minAndMax(array:[Int]) -> (min:Int ,max:Int){

    var currentMin = array[0]

    var currentmax = array[0]

    for value in array[1..<array.count] {

        if value < currentMin{

            currentMin = value

        }else if value > currentmax{

            currentmax = value

        }

    }

    return (currentMin,currentmax)

}


let value = minAndMax(array: [2,4,56,7,8,9,-3])

//取出最大值和最小值

value.min

value.max


//f、返回值为空的情况如何处理:返回元组使用可选类型

func minAndMax1(array:[Int]) ->(min:Int ,max:Int)?{

    if array.isEmpty {

        return nil

    }

    var currentMin = array[0]

    var currentmax = array[0]

    for value in array[1..<array.count] {

        if value < currentMin{

            currentMin = value

        }else if value > currentmax{

            currentmax = value

        }

    }

    return (currentMin,currentmax)

}


//3、函数的参数标签和参数名称 :frist\soncond是参数名称,age \ number是参数标签,参数标签和参数名称中间用空格隔开,如果定义了参数标签,在调用的时候只跟参数标签

func tagFunction(age frist:Int,number soncond:Int){

    

}


tagFunction(age: 12, number: 45)


func tagFunction(frist:Int,number soncond:Int){

    

}

tagFunction(age: 23, number: 4)


//4、默认参数值

func defualtValue(first:Int ,second:Int = 12){

    

}

defualtValue(first: 12, second: 3)


defualtValue(first: 12)//如果设置了默认值而且调用的时候没有重新给值,那就是使用默认值

//可变参数:

func canChangeFunction(_ numbers:Double...) ->Double{

    var total:Double = 0

    for number in numbers {

        total += number

    }

    return total / Double(numbers.count)

}


canChangeFunction(2,4,5,67,8,9,6)


//5、输入和输出的参数:修改传入参数


var aa = 3

var bb = 5


func inputAndoutput(_ a:inout Int, _ b:inout Int){

    let tem = a

    a = b

    b = tem

}


inputAndoutput(&aa, &bb)


print(aa)

print(bb)


//6、函数类型:

func addFunction(_ a:Int,_ b:Int) -> Int{

    return a + b

}


func multiplyFunction(_ a:Int,_ b:Int) ->Int{

    return a * b

}

//这两个函数的函数类型是:(IntInt)-> Int



func helloWorld(){

print("hello world!")

}


//上面函数的函数类型是:()->Viod

//2)、函数类型的使用:

var mathFunction:(Int,Int) -> Int = addFunction

//定义一个mathFunction的变量,类型是(IntInt)->Int ,然后将新的变量指向addFunction函数


mathFunction(2,4)

print(mathFunction(2,4))


mathFunction = multiplyFunction//系统可以推断函数类型

print(mathFunction(2,4))


//3)、函数类型作为参数类型

//可以把(Int,Int)->Int这个函数类型作为另一个函数的参数类型,目的:将一个函数的一部分分实现留给该函数的调用者来实现

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

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

}


printResult(addFunction, 2,4)


//定义了一个printResult_:_:_:)函数,第一个参数是mathFunctionmathFunction的类型是:(Int,Int)->Int,这里可以传入任意一个相同类型的函数,第二和第三个参数ab都是Int型的数据,我们把这两个值作为给定函数的输入值


//4)、函数类型作为返回类型


func addOne(_ input:Int) ->Int{

    return input + 1

}


func jianOne(_ input:Int) ->Int{

    return input - 1

}


func chooseFunction(back:Bool) ->(Int) ->Int{

    return back ? addOne : jianOne

}

var currentNumber = 2


let xxx = chooseFunction(back: currentNumber > 0)

print(xxx)


//5)、嵌套函数:以上放置在全局的所有函数称作全局函数,因为他们定义在全局域中,如果我们把函数定义在其他函数的函数体中,我们就把它称为嵌套函数


func meorFunction(back:Bool)->(Int) ->Int{

    func addOne(_ input:Int) ->Int{

        return input + 1

    }

    func jianOne(_ input:Int) ->Int{

        return input - 1

    }

    return back ? addOne : jianOne

}


var currValue = 2

let result = meorFunction(back: currValue > 0)

print(result)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值