美文网首页
swift:函数Function

swift:函数Function

作者: 娘亲Joanna | 来源:发表于2016-06-13 16:42 被阅读119次

//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)
}


相关文章

  • swift:函数Function

  • Swift 5 函数 function

    函数 隐式返回: 函数体是单一表达式,函数可以隐式返回这个表达式func sumfunc(v1: Int, v2:...

  • Swift4.0 函数(Function)

    针对swift4.0函数做一些笔记。 普通函数,不做过多笔记 可变参数函数 (Variadic Parameter...

  • Swift Function Builder

    Swift 5.1开始,Swift多了一个Function Builder的特性。Function Builder...

  • Swift anonymous function 函数 省略规则

    一直在用OC写代码,对swift没有什么研究,一直想着不就一个新语言嘛,到用的时候顺便学习一下规则就行了,api那...

  • JS中的函数

    函数: function(){} 函数声明: function name(){ } 函数执行: name(); 函...

  • shell函数工作实战应用

    shell函数:function函数定义:[function] funcname(){Statement;} 注意...

  • No.23 JavaScript函数

    一、函数的使用 1. 声明函数 // 声明函数function 函数名() {//函数体代码} function ...

  • Function (swift)

    2018-05-30 (1) 函数定义与调用/// func 函数名 (函数形参列表) -> 返回类型 {函数体}...

  • Function Swift

    本次分享目的 让大家对 Functional Programming有一个基本的了解 熟悉Swift Librar...

网友评论

      本文标题:swift:函数Function

      本文链接:https://www.haomeiwen.com/subject/mmmydttx.html