zoukankan      html  css  js  c++  java
  • Swift

    1,无返回值的函数

     func test(name:String){
            print(name)
        }

     2,返回一个返回值

     func test(name:String) -> String{
           return "Hello" + " " + name
        }
    
     print(test(name: "baibai")) //Hello baibai
    

     3,返回由多个值组成的复合返回值

    func test(name:String) -> (Int,Bool){
           let postion = 1
           let visible = false
            return(postion,visible);
            
        }
    

     4,可变形参:可以接受0个或者任意数量的输入参数

       func test(numbers:Int...) -> Int {
            var count:Int = 0
            for number in numbers{
                count += number
            }
            return count
        }
     print(test(numbers:1,2,10))//13
    

     5,如果想要同时改变函数内外的参数值,可以利用inout关键字,同时调用函数的时候给参数加上前缀“&”

     func add(age:inout Int) -> Void {
            age += 1
        }
     var age = 22
            add(age: &age)
            print(age)//23
    

     6,可以使用函数类型的参数

    func additive(a:Int,b:Int) -> Int {
            return a + b
        }
    
        //函数类型的参数
        func printAdditiveResult(addFun: (Int, Int) -> Int, a:Int, b:Int){
            print("Result:(addFun(a,b))")
        }
    printAdditiveResult(addFun: additive, a: 5, b: 7)//Result:12
    

     7,也可以使用函数类型的返回值

    //定义个自增函数
        func increase(input:Int) -> Int {
            return input + 1
        }
        
        //定义个自减函数
        func reduce(input:Int) -> Int {
            return input - 1
        }
        
        //定义一个返回函数类型的函数
        func chooseFunction(backwards:Bool) -> (Int) -> (Int){
            
            return backwards ? reduce : increase
        }
    
    
    let aFun = chooseFunction(backwards:true)
           print(aFun(3))//2
    
  • 相关阅读:
    bootstrap class sr-only 什么意思?
    PHP 中的文本截取分析之效率
    FastAdmin 升级后出现 is already in use
    FastAdmin 环境变量 env 配置
    Nginx 服务器伪静态配置不当造成 Access denied
    笔记:明确认识
    进程通信(socket)
    进程间通信(了解)
    c++ 继承,组合
    c++ 类初始化
  • 原文地址:https://www.cnblogs.com/baidaye/p/8624063.html
Copyright © 2011-2022 走看看