Swift First Sight Swift function (i)

Source: Internet
Author: User

In any language, functions are very important, and today we are going to take a preliminary study of the functions in swift.

1. Swift function Default parameters:

①, can be placed in any position of the function;

②, external name and internal name are consistent;

③, call time can not write;

④, defined in the parameters passed in when the function is defined.

Func joinstring (s1:string, toString s2:string, joiner s3:string = "*"), string{    return s1 + s3 + S2}let str = Joinstring ("Hello", ToString: "World", Joiner: "#") println (str)


As can be seen from the above code, joiner is defined as a default parameter, we can write in the call or do not write, see the following code:

Func joinstring (s1:string, toString s2:string, joiner:string = "*"), string{    return s1 + joiner + S2}let St r = joinstring ("Hello", ToString: "World", Joiner: "#") println (str)


At this point the joiner is both an external parameter and an internal parameter, which is equivalent to adding the # number in front of the joiner.


2. Swift Input/Output function (inout)

Func swap (Var a:int, var b:int) {let    t = a    a = b    b = T}var x = 10var y = 19swap (x, y) println ("\ (x), \ (y)")


As can be seen from the above code output, the value of x and y actually does not change, this is a misunderstanding we know that the swap parameter is actually an argument, it is equivalent to the input of 2 integers, and actually x and Y values have not been changed, if we need to change the value of x and Y, Then we should pass in the address of X and Y.

Func swap (inout a:int, inout b:int) {let    t = a    a = b    b = T}var x = 10var y = 19swap (&x, &y) println (" \ (x), \ (y) ")


As can be seen from the code above, when we need to change the actual value of the variable, we should use the input and output function (inout) when the function is called, and also add the & symbol when calling the function.


Conclusion: When we need to modify the external function inside the function, we need to change the function parameter position to the InOut argument, and call the function to take the address.






Swift First Sight Swift function (i)

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.