swift:函数Function


//Functions

//带参数的函数
func sayHello(personName: String) ->String{
    
let greeting = "Hello, " + personName + "!"
    
return greeting
}


sayHello("Cjoe")

//不带参数是的函数
func sayHello2() ->String{
return "hello world"
}
sayHello2()


//带多个参数的函数
func sayHello1 (personName:String, alreadyGreeted:Bool) ->String{
    if alreadyGreeted{
        return sayHello(personName)
    }else{
        return sayHello1("Tim", alreadyGreeted: true)
    }
}
sayHello1("Cjoee", alreadyGreeted: false)

//有参数无返回值
func sayGoodbye(presonName: String){
       print("Goodbye, \(presonName)!")
}
sayGoodbye("Cjoee")

func sayGoodbye1(presonName: String) ->(){
    print("Goodbye, \(presonName)!")
}
sayGoodbye1("Cjoee")

func sayGoodbye2(presonName: String) ->Void{
    print("Goodbye, \(presonName)!")
}
sayGoodbye2("Cjoee")


//有参数有一个返回
func printAndCount(stringToPrint: String) -> Int {
    print(stringToPrint)
    return stringToPrint.characters.count  //2
}
func printWithoutCounting(stringToPrint: String) {
    printAndCount(stringToPrint) //12
}
printAndCount("hello, world")
// prints "hello, world" and returns a value of 12
printWithoutCounting("hello, world")
// prints "hello, world" but does not return a value


//有参数有多个返回值
func minMax(array: [Int]) ->(min: Int, max:Int){
var currentMIn = array[0]  //23
    var currentMax = array[0] //23
    for value in array[1..<array.count]{
        if value < currentMIn{
        currentMIn =  value
        
        }else if value > currentMax{
        currentMax = value
        }
    }
return (currentMIn,currentMax)
}
minMax( [23, 33 , 12 , 45 , 12 , 2 ]) //(.0 2, .1 45) 返回的是一个元组


let bounds = minMax([23,3,23,2345,55])
print("min is \(bounds.min) and max is \(bounds.max)") //"min is 3 and max is 2345\n"

//参数为int
func someFunction(firstParameterName: Int, secondParameterName: Int) {
    // function body goes here
    // firstParameterName and secondParameterName refer to
    // the argument values for the first and second parameters
}
//someFunction(firstParameterName: 1, 2)  false  第一个参数的参数名必须省略 第二个不可以
someFunction(1, secondParameterName:2)


//参数为string
func sayHello(to person: String, and anotherPerson: String) -> String {
    return "Hello \(person) and \(anotherPerson)!"
}
print(sayHello(to: "Bill", and: "Ted")) //两个标志不可省略 参数名可以省略
// Prints "Hello Bill and Ted!"

//如果想省略函数名  和标志  使用下划线underScore
func someFunction (firstParameterName:Int, _ secondParameterName:Int){

}
someFunction(1, 3)

//设置参数的默认值
func someFunction1(parameterWithDefault: Int = 12){
parameterWithDefault  //6, 12
}
someFunction1(6)
someFunction1()

//numbers: Double...代表numbers的个数为参数的个数
func arithmeticMean(numbers: Double...) ->Double{
    var total: Double = 0
    for number in numbers{
    total += number
    }
return total / Double(numbers.count)
}
arithmeticMean(1,3,4,4,5)

//交换值 inout  inout _
func swapTwoInts(inout a: Int, inout _ b: Int){
let temporaryA = a
    a = b
    b = temporaryA
}
var someInt = 3
var  anotherInt = 14
swapTwoInts(&someInt, &anotherInt)



func addTwoInts(a: Int, _ b:Int) -> Int{
return a + b
}
addTwoInts(23, 1)

//_代表函数调用的时候省略函数的提示词 以及函数名称
func multiplyTwoInt(a:Int, _ b: Int) -> Int{
return a * b
}
multiplyTwoInt(2, 3)



func printHelloWorld(){
print("hello word")
}
printHelloWorld()

//---------------------------------------care for -----------------------------------------
//返回函数类型 mathFunction 的参数类型和addTwoInts 的一样  所以可以吧addTwoInts的方法赋给 mathFunction
var mathFunction:(Int,Int) ->Int = addTwoInts
mathFunction(3,6)//9

mathFunction = multiplyTwoInt
mathFunction(3,7) //21

let anotherFunction = addTwoInts
anotherFunction(3,8) //11


//函数类型作为参数类型
func printMathResult(mathFunction :(Int, Int) -> Int, _ a:Int, _ b: Int){
print("result: \(mathFunction(a ,b))") // 5
}
printMathResult(addTwoInts , 2, 3)



//函数类型作为范围类型
func stepFoword(input: Int) ->Int{
return input + 1
}

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

func chooseStepFunction(backwards: Bool) ->(Int) ->Int{  //(Int) ->Int 代表的是一个函数类型  (backwards: Bool)函数参数
    return backwards ? stepBackForword : stepFoword
}

var currentValue = 3
let moveNearerZero = chooseStepFunction(currentValue > 0)

while currentValue != 0 {
    currentValue = moveNearerZero(currentValue)
}
//print("zero!")

//嵌套函数
func chooseStepFunction1(backwards :Bool) ->(Int) ->Int{

    func stepForward(input: Int) ->Int{
    return input + 1
    }
    
    func stepFoword(input: Int) ->Int{
    return input - 1
    }
    
    return backwards ? stepBackForword : stepFoword

}

var currentValue1 = -4
let moveNearerToZero = chooseStepFunction(currentValue1 > 0)
// moveNearerToZero now refers to the nested stepForward() function
while currentValue1 != 0 {
    print("\(currentValue1)... ")
    currentValue1 = moveNearerToZero(currentValue1)
}


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容