• swift语言点评五-Function


    一、函数类型

    Every function in Swift has a type, consisting of the function’s parameter types and return type.

    Function Types

    Every function has a specific function type, made up of the parameter types and the return type of the function.

    For example:

    1. func addTwoInts(_ a: Int, _ b: Int) -> Int {
    2. return a + b
    3. }
    4. func multiplyTwoInts(_ a: Int, _ b: Int) -> Int {
    5. return a * b
    6. }

    This example defines two simple mathematical functions called addTwoInts and multiplyTwoInts. These functions each take two Int values, and return an Int value, which is the result of performing an appropriate mathematical operation.

    The type of both of these functions is (Int, Int) -> Int. This can be read as:

    “A function that has two parameters, both of type Int, and that returns a value of type Int.”

     

    Using Function Types

    You use function types just like any other types in Swift. For example, you can define a constant or variable to be of a function type and assign an appropriate function to that variable:

    1. var mathFunction: (Int, Int) -> Int = addTwoInts

    This can be read as:

    “Define a variable called mathFunction, which has a type of ‘a function that takes two Int values, and returns an Int value.’ Set this new variable to refer to the function called addTwoInts.”

     

    Function Types as Parameter Types

    Here’s an example to print the results of the math functions from above:

    1. func printMathResult(_ mathFunction: (Int, Int) -> Int, _ a: Int, _ b: Int) {
    2. print("Result: (mathFunction(a, b))")
    3. }
    4. printMathResult(addTwoInts, 3, 5)
    5. // Prints "Result: 8"

    Function Types as Return Types

    右侧结合律

    Function Argument Labels and Parameter Names

    Each function parameter has both an argument label and a parameter name. The argument label is used when calling the function; each argument is written in the function call with its argument label before it. The parameter name is used in the implementation of the function. By default, parameters use their parameter name as their argument label.

    Specifying Argument Labels

    1. func greet(person: String, from hometown: String) -> String {
    2. return "Hello (person)! Glad you could visit from (hometown)."
    3. }
    4. print(greet(person: "Bill", from: "Cupertino"))

    关键字:

    In-Out Parameters

    Function parameters are constants by default. Trying to change the value of a function parameter from within the body of that function results in a compile-time error.

    Optional Tuple Return Types

    An optional tuple type such as (Int, Int)? is different from a tuple that contains optional types such as (Int?, Int?). With an optional tuple type, the entire tuple is optional, not just each individual value within the tuple.

    func minMax(array: [Int]) -> (min: Int, max: Int)?

    返回值需要判断

     

     

  • 相关阅读:
    51keil编译器printf函数
    asp.net里登陆记住密码
    Asp.net GridView分页
    DataTable拆分分页
    ASP.NET MVC 窗体身份验证及角色权限治理示例
    asp.net获取IP地址
    Asp.net Ajax框架教程
    将页面的ViewState放在Session
    20个Jquery表单插件
    前端下载图片的N种方法
  • 原文地址:https://www.cnblogs.com/feng9exe/p/8709081.html
Copyright © 2020-2023  润新知