zoukankan      html  css  js  c++  java
  • swift基础02

    函数

    声明函数关键字 func,  

    语法: func 函数名(参数) -> 返回值{}
    eg:
    //无参无返回值 func frist(){ print("这是我的第一个函数") } 调用 :func()

    //无参,返回值String
    func frist2() -> String {
      return "abc"
    }
    //一个Int类型的参数公园 ,返回值是Int类型
    func first3(a:int) -> Int {
      print(a)
    return a+1
    }
    调用 : frist3(a:4)  //frist3(5) 报错,参数名不能省略, 想要忽略参数名,需要在声名参数名时候,在前面在 “_ ” 注意"_"后面必须跟一个空格,否则也是报错
    eg:

    func first4( _ a:int) -> Int {
      print(a)
    return a+1
    }

    func add(firstNumber: Int ,addWithSecond: Int) -> Int {
     return firstNumber + addWithSecond
    }
     add(firstNumber: 3, addWithSecond: 4)
     add(addWithSecond: 3,firstNumber: 4)//不能改变顺序
      

    //a,b成为外部参数,argument label(参数标签),
    ab不能在函数体内使用,只能调用时使用

    func add(a firstNumber: Int ,b addWithSecond: Int) -> Int {
        return firstNumber + addWithSecond
    }
    add(a:5, b: 6)


    /**********可变长度,默认值***********/
    //参数可变长度 ,和java ..相同
    func add(numbers: Int...) -> Int {
      var total = 0
      for item in numbers{
        total += item
      }
       return total
    }
    add(numbers: 1,23,4,5,6)
    //参数有默认值
    func add(num1: Int =2, num2: Int = 3) -> Int{
        return num1 + num2
    }

    func add2(num1:Int =2, num2: Int = 3, num3: Int) ->Int{
      return num1 + num2 + num3
    }

     函数在swift中是一等公民,意味着与类同级别

    let f: (Int,Int) -> Int
    func test1(a:Int, b:Int) -> Int {
         return a + b   
    }
    f = test1
    f(5, 6)
    
    let f2: ()-> Void
    func test2(){
       print("asd")  
    }
    fe = test2
    f2()
    

      

    func outer(inner:(Int) -> Void){  //以函数作为参数,无返回值
       inner(5)  
    }
    
    func test3(a :Int){
        print(a)      
    }
    
    outer(inner: test3)  //调用
    
    函数Demo
    func demo(doFilter:(Int) -> Bool) -> [Int]{
            let arr = [11,54,88,8]
            var result : [Int] =[]
            for item in arr{
               if doFilter(item){
                     result.append(item)
                }
            }    
         return result  
    }
    
    func goulv(a: Int) -> Bool {
        if a>30 {
            return true
        }  
         return false  
    }
    
    let rr= demp(doFilter: goulv)
    

      /***模仿JQuery 里面的eache***/

    func each(handler: (Int,Int)->Void) {
        let arr = [1,2,6,8,99,6,33]
        
        for i in 0..<arr.count {
            handler(i,arr[i])
        }
    }
    
    func myHandler(index: Int,value: Int)  {
        print("index is (index) value is (value)")
    }

    each(handler: myHandler)
    func aaa() -> Void {
           print("this fun is called outer")             
    }     
    
    func outer2 - > () ->Void{
        return aaa
    }    
    
    func outer3() ->() ->Void{
        func bbb() -> Void{
            print("this fun is callled outer")
        }
        return bbb
    }        
    let recFun = outer()
    reFun()
    outer3()()  //outer3是一个无参,返回值为函数的函数,  调用的时候需要在函数后面在加()
    

     

    func outer4(f:()->Void) -> (Int,Int)->(Int,Int) {
        
        f();
        func temp(a: Int,b: Int)-> (Int,Int) {
            
            return(a + b,a * b)
        }
        return temp
    }
    func forOuter4() {
        print("for outer4")
    }
    let outer4Result  = outer4 (f:forOuter4)
    let outer4ResultResult = outer4Result(5,6)
    outer4ResultResult.0
    outer4ResultResult.1
    

      

      

      

  • 相关阅读:
    CodeForces 7B
    CodeForces 4D
    离散化
    线段树入门
    洛谷 P3951 小凯的疑惑(赛瓦维斯特定理)
    Codeforces 1295D Same GCDs (欧拉函数)
    Codeforces 1295C Obtain The String (二分)
    Codeforces 1295B Infinite Prefixes
    Codeforces 1295A Display The Number(思维)
    Codeforces 1294F Three Paths on a Tree(树的直径,思维)
  • 原文地址:https://www.cnblogs.com/LiaoYJ/p/6080895.html
Copyright © 2011-2022 走看看