&-in Swift

Source: Internet
Author: User

Passing references to parameters

A class is a reference type, and other data types such as Integer, float, Boolean, character, string, tuple, collection, enumeration, and struct are all value types.

Sometimes it is possible to pass a value type parameter as a reference, which is achievable, and theinout keyword provided by Swift can be implemented. Look at one of the following examples:

[HTML]View PlainCopyprint?
  1. Func Increment (inout value:double, amount:double = 1.0) {
  2. Value + = Amount
  3. }
  4. var value: Double = 10.0
  5. Increment (&value)
  6. Print (value)
  7. Increment (&value, amount:100.0)
  8. Print (value)

Code Increment (&value) is the call function increment, the increment is the default value, where &value (add & symbol before the variable , take out the value address) is the way to pass the reference , when defining a function, the parameter identification corresponds to the inout.

Code Increment (&value,amount:100.0) is also called function increment, and the increase is 100.0.

The above code output results are as follows:

11.0

111.0

&-in Swift

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.