Swift Learning Note 2

Source: Internet
Author: User

1. The function parameter has an external parameter name (external parameter name) and a local parameter name (local parameter name). The external parameter name is used to label the arguments passed to the function when the function is called, and the local parameter names are used inside the implementation of the function.

In general, the first parameter omits its external parameter name, and the second and subsequent arguments use its local parameter name as the external argument name.

If you provide an external parameter name, the function must use the external argument name when it is called.

If you do not want to set an external parameter name for the second and subsequent parameters, use an underscore ( _ ) instead of an explicit parameter name.

2. Variable parameters

func arithmeticMean(numbers: Double...) -> Double {    var total: Double = 0 for number in numbers { total += number } return total / Double(numbers.count)}

3. Function parameters are constants by default. Attempting to change the value of a parameter in the function body causes a compilation error. This means that you cannot change the value of a parameter by mistake. Define variable parameters by adding a keyword before the parameter name var . The variable parameter is not a constant, and you can use it as a new modifiable copy in the function.

4. When defining an input/output parameter, add the keyword before the parameter definition inout . An input-output parameter has the value of the passed-in function, which is modified by the function, and then the outgoing function replaces the original value. You can only pass variables to the input and output parameters. You cannot pass in constants or literal literals (literal value), because these quantities cannot be modified. When an incoming parameter is used as an input and output parameter, it is necessary to add a character before the parameter name & , indicating that the value can be modified by the function.

5. Closures are self-contained function code blocks that can be passed and used in code. Closures in Swift are similar to the code blocks in C and objective-c (blocks) and anonymous functions in some other programming languages. Closures can capture and store references to any constants and variables in the context in which they are located. This is called closed merge wrapped with these constants and variables, commonly known as closures.

Closures take one of the following three forms:

    • A global function is a closed packet that has a name but does not capture any value
    • A nested function is a closure that has a name and can capture values within its enclosing function domain
    • A closure expression is an anonymous closure that uses lightweight syntax to capture variables or constant values in its context.

4.

Swift Learning Note 2

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.