SWIFT-31-constant parameters, variable arguments, and inout parameters

Source: Internet
Author: User

: Playground-noun:a place where people can playimport uikit//Swift by default, the parameters passed in are not modifiable, that is, the let type, which is the constant parameter//if you want to modify the parameter         Value, you need to add "var" to the parameter, that is, the variable parameter, func tobinary (Var num:int), String//Converts a number to binary {var result = "" while num! = 0 { result = String (num%2) + result num/= 2} return result}var number = 69toBinary (number) Number/ /input result or 69, explain the function of the variable parameter modification, does not affect the external variable itself//the following two function also proves that the variable parameter does not affect the external variable itself (including arrays and dictionaries) var a = 1func testchangeintfunction (var a:int {a = 0}testchangeintfunction (a) Avar arr = [2, 3]func Testchangeintfunction (var a:[int]) {a = [3, 4]}testchangeintf Unction (arr) arr/*--------------------------------------------------------------------*///So how do you change the external variable itself within a function? Here is the use of another keyword "inout"//This can be understood as a pointer to the parameter (that is, memory address) func Changeeachother (inout a:int, inout b:int) {Let temp = a A =  b b = Temp}var x = 0, y = 100print ("x = \ (x), y = \ (y)") Changeeachother (&x, B: &y) print ("x = \ (x), y = \ (y)")// Swift's own swap function//swap (&< #T # #a:t# #T #&gt, &< #T # #b: t# #T #>) 

  

SWIFT-31-constant parameters, variable arguments, and inout parameters

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.