Swift Learning-functions

Source: Internet
Author: User

Knowledge Points:

    1. different points of Swift functions
    2. Three ways to do a function without a return value
    3. Returns the tuple data
    4. Name of external parameter
    5. Shorthand for external parameter names
    6. Default parameter value, when there is a default parameter value, the system automatically treats the parameter name as an external parameter name.
    7. With default parameter values, add an underscore _ and the system ignores the external parameter name
    8. The parameter of a function is a constant by default, and you want to change the value of the parameter inside the body of the function and set it to Var
    9. Input and output parameters and their points of attention
Example code:

: Playground-noun:a place where people can playimport uikit//sum function name, NUM1 and num2 parameter names, Int after arrow is return type Func sum (Num1:int, Nu M2:int), Int {return num1 + num2}sum (1, 2)//three notation for functions with no return value func noRe1 (), Void {}func noRe2 (), () {}func NoRe3 () {}//Returns the tuple data func getpoint (), (int, int) {return (Ten)}func Getperson (id:int), name:string, Age: INT) {If id > 0 {return ("Jack")} else {return ("Nobody",}}var person = Getperson (10 ) println ("Name: \ (person.name), Age: \ (person.age)")//external parameter name, where the external parameter name is set separately for Name,age,no, stu_name,stu_age,stu_no//function, Easy to read func addstudent (stu_name name:string, Stu_age age:int, Stu_no no:int) {println ("name: \ (name), Age: \ (age), No:\ (n o) ")}addstudent (stu_name:" Jack ", Stu_age:10, stu_no:20)//shorthand for the external parameter name, preceded by the # Sign in front of the internal formal parameter name, so that the internal parameter name can also act as the external parameter name func AddStudent2 (#name: String, #age: int, #no: int) {println ("name: \ (name), age: \ [age], no:\ (NO)")}addstudent2 (name: "To M ", age:20, no:222)//default parameter value//This gives age a default value,At the time of the call, do not write or//the formal parameter with the default parameters, the system will give it an external parameter name, and the internal parameters of the same name Func AddStudent3 (#name: String, age:int =) {println ("name: \ (name) , Age: ' (age) ')}addstudent3 (name: "Smith") AddStudent3 (name: "Hello", age:10)//Add an underscore before the default parameter name, the system ignores the external parameter name func AddStudent4 (name:string, _ Age:int =) {println ("name: \ (name), Age: \ (age)")}addstudent4 ("A", 2)//The parameter of the function is a constant by default, if you want to         Modify the value of the parameter inside the function, set the parameter to Var to func Addstr (Var initstr:string, suffix:string, Num:int), String {for _ in 0..<num { Initstr + = suffix} return initstr}addstr ("A", "B", 10)//input/output parameter, similar to the value of the variable in the address change function in c/* Note: 1, cannot pass in constant and literal (such as 10) number , because they are immutable 2, passing parameters, you must precede the arguments with &3, input and output parameters cannot have a default value of 4, input and output parameters can not make the variable parameter 5, input and output parameters can no longer use Let, var decoration (inout and allow, VAR cannot coexist) */ Func Change (inout num:int) {num = 10}var a = 20//general input and output parameters, the call is preceded by an increase in the & symbol change (&a) println (a)//How to use the third parameter case, exchange two number func swap (inout a:int, InOut b:int) {a = a + b = A-b = a-b}//algorithm 2func swap1 (inout a:int, InOut b : Int) {//XOR or operation characteristics, the same will counteract a = a ^ b b = a ^ b A = a ^ B}var Temp1 = 10var Temp2 = 20swap (&AMP;TEMP1, &AMP;TEMP2) println ("Temp1 = \ (temp1), Temp2 = \ (temp2)") v Ar Temp3 = 30var temp4 = 40swap1 (&temp3, &temp4) println ("Temp1 = \ (Temp3), Temp2 = \ (temp4)")//input/Output function value//example: No  With the return value can also be calculated and with the difference//have the return value of the notation func sumandminus (Num1:int, Num2:int), (Sum:int, Minus:int) {return (NUM1 + num2, NUM1- NUM2)}var result = Sumandminus (Ten) result.sumresult.minus//input/output function syntax func sumAndMinus2 (Num1:int, Num2:int, inout sum: Int, inout minus:int) {sum = Num1 + num2 minus = Num1-num2}var sum1 = 0var Minus1 = 0sumandminus2 ($, &AMP;SU M1, &minus1) Sum1minus1




Swift Learning-functions

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.