swift 5.x 函数

//
//  ViewController4.swift
//  swiftT
//
//  Created by wjwdive on 2020/5/19.
//  Copyright © 2020 wjwdive. All rights reserved.
//

import UIKit

class ViewController4: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "函数 - function"
        //函数的定义和调用
        
        //实际参数标签 参数 函数示例
        addFunction(first: 10, second: 20)
        
        let mathFunction: (Int, Int) -> Int = addTwoInts
        print(mathFunction(1, 1))
        
        // 内嵌函数实例
        var currentValue = -5
        let stepFunc = chooseStepFunctionInnerFunc(backwords: currentValue > 0)
        while currentValue != 0 {
            currentValue = stepFunc(currentValue)
        }
        print(currentValue)
        
        
    }
    
    
    //函数,求一个数组的最大值和最小值, 元组返回
    func minMax(array: [Int]) -> (min: Int, max: Int)? {
        if array.isEmpty {
            return nil
        }
        var minValue = array[0]
        var maxValue = array[0]
        for value in array {
            if value < minValue {
                minValue = value
            }
            if value > maxValue {
                maxValue = value
            }
        }
        return (minValue, maxValue)
    }
    
    
    //形式参数名,实际参数标签
    
    //使用下划线来省略 实际参数标签
    func someFunction(_ firstParameterName: Int, secondParameterName : Int) {
        
    }
    
    // 调用的时候,必须用实际参数标签 first, second
    func addFunction(first firstParameterName: Int,second secondParameterName : Int) {
           print("add result ",firstParameterName + secondParameterName)
    }
    
    
    
    //形式参数默认值
    // 通过在形式参数类型后给形式参数赋值来给函数的任意形式参数定义一个默认值
    // 如果定义了默认值,就可以在调用的时候省略这个形式参数
    
    //可变形式参数
    // 一个可变形式参数可以接受0或多个特定类型的值,当调用函数的时候可以利用可变形式参数来声明形式参数可以被传入值的数量是可变的。可以通过在形式参数的类型名称后边插入三点符号(...)来书写可变形式参数
    //传入到可变形式参数中的值在函数的主体中被当作是对应类型的数组
    func arithmeticMean(_ numbers: Double...) -> Double {
        var total: Double = 0
        for number in numbers{
            total += number
        }
        return total / Double(numbers.count)
    }
    
    // 输入输出形式参数
    // 可变形式参数只能在函数内部做改变,如果你想函数能够修改一个形式参数的值,而且你想改变在函数结束之后依然生效,那么就需要将形式参数定义为输入输出形式参数
    // 在形式参数定义开始的时候在前边添加一个 inout 关键字可以定义一个输入输出 形式参数。输入输出形式参数有一个能输入给函数的值,函数能对其进行修改,还能输出到函数外边替换原来的值
    
    // 你只能把变量作为输入输入出形式的实际参数,在将变量作为实际参数传递给输入输入出形式参数的时候,直接在它前边添加一个和符号(&),来明确可以被函数修改
    // 输入输出形式参数不能有默认值,可变形式参数不能被标记为inout
    func swapTwoInts(_ a: inout Int, _ b: inout Int) {
        let temp = a;
        a = b;
        b = temp;
    }// 函数类型是 (Int,Int)->() 而不是 Int,Int)-> Void
    
     
    // 1、函数类型
    // 每个函数都有一个特定的函数类型,它由形式参数类型,返回类型组成
    // 例如下面两个函数的类型均为 (Int, Int) -> Int
    func addTwoInts(_ a: Int, _ b: Int) -> Int {
        return a + b
    }
    
    func mutiplyTwoInts(_ a: Int, _ b: Int) -> Int{
        return a * b
    }
    
    // 2、使用函数类型
    // 可以像使用swift中的其他类型一样使用函数类型, 例如,可以给一个常量或变量定义一个函数类型,并且为变量指定一个响应的函数
    
    //例如
    //let mathFunction: (Int, Int) -> Int = addTwoInts
    //print(mathFunction(2, 3))
    
    
    //3、函数类型作为形式参数的类型
    //可以使用函数类型(例如 : (Int, Int) -> Int )作为其他函数的形式参数类型。这允许你预留函数的部分实现从而让函数的调用者在调用函数的时候提供
    func printMathResult(_ mathFunction:(Int, Int) -> Int, _ a: Int, _ b: Int) {
        print("Result: (mathFunction(a, b))")
    }
    //printMathResult(addTwoInts, 1, 1)
    
    //4、函数的类型作为函数的返回值
    // 可以利用函数的类型作为另一个函数的返回类型。写法是在函数的返回箭头(->)立即写一个完整的函数类型
    func stepForward(_ input: Int) -> Int {
        return input + 1
    }
    func stepBackword(_ input: Int) -> Int {
        return input - 1
    }
    
    func chooseStepFunction(backwords: Bool) -> (Int) -> Int {
        return backwords ? stepBackword : stepForward
    }
    
    
    // 内嵌函数
    // 可以在函数内部定义另一个函数,
    //
    func chooseStepFunctionInnerFunc(backwords: Bool) -> (Int) -> Int {
        func stepForward(_ input: Int) -> Int {return input + 1}
        func stepBackword(_ input: Int) -> Int {return input - 1}
        return backwords ? stepBackword : stepForward
    }
    
    
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

原文地址:https://www.cnblogs.com/wjw-blog/p/12921803.html