zoukankan      html  css  js  c++  java
  • 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)?

    返回值需要判断

     

     

  • 相关阅读:
    docker for zabbix
    由阿里云宕机引发的思考
    linux清空文件
    RDS导入注意事项
    coreos 常见问题
    2018年7月28日笔记
    2018年7月26日笔记
    2018年7月24日笔记
    作业五之系统设计时所实现的质量属性战术
    作业四之阅读《大型网站技术架构:核心原理与案例分析》第五、六、七章
  • 原文地址:https://www.cnblogs.com/feng9exe/p/8709081.html
Copyright © 2011-2022 走看看