Swift: Functions and methods

Source: Internet
Author: User

Before we introduce the functions and methods in swift, let's take a look at the notation of functions and methods in objective-c for the example of two numbers:

1. Function notation

int sum (int a, int b) {    return a + b;}

2. Method notation

-(int) sum: (int) a B: (int) b {    return a + b;}

As can be seen from the above, the wording of the two are still very different. And in Swift, the two are directly unified, the following wording:

Func sum (a:int, b:int), Int {    return a + b;}

Because the two are unified, so the following description, I unified called the method.


The definition format of a method

Func method Name (parameter name: parameter type, ...), return value type {

}


Ii. several forms of the method

1. Methods with no return value

Func test (Num1:int), void{    }

or

Func test (Num1:int), () {    }

or

Func Test (num1:int) {}

2. Methods with no formal parameters

Func test (), int{    return 0}

3. Methods that return multiple values, that is, the form of a tuple

Func Find (Id:int) (name:string, age:int) {    If ID > 0 {        return ("Jack")    } else {        return ("R OSE ","    }}var person = find (2) println ("Name=\ (Person.name)", Age=\ (Person.age))

4. Variable parameter list (using params from C #)

Func sumof (Numbers:int ...), Int {    var sum = 0 for number in    numbers {        sum + = number    }        return Sum}//sumof ()  results: 0//sumof (1,2,3,4) The result is: 10

iii. Types of formal parameters

1. External parameter name (define a name before the formal parameter, so that you have a name hint when calling the method)

Func Test (Number1 num1:int,number2 num2:int), int{    return NUM1 + num2}test (number1:3, Number2:5)

Notice that the names Number1 and Number2 are added before the arguments num1 and num2, so that when the method test is called, the prompt name of the parameter you are going to enter is automatically prompted.


2. Hint name is consistent with formal parameter name, precede form parameter with #

Func Test (#num1: int, #num2: int), int{    return NUM1 + num2}test (num1:3, Num2:6)


3. Default parameter value (that is, a parameter in the method has a default value)

Func addstudent (name:string, age:int =) {    println ("Add Student: name=\ (name), age=\ (age)")}

As can be seen from the example above, the age parameter has a default parameter value of 20. It means that when you call the method, if you pass in the value of age, you will overwrite the value of 20, and if you pass only the value of name, then the value of "is".

The result of the call is as follows:

Addstudent ("Lifei")        //Add student: Name=lifei, Age=20addstudent ("Jack", age:30)  //Add student: Name=jack, age=30

As you can see from the above invocation form, the parameter of the default parameter value is in the form of an external parameter name, because it appears as the age hint.


4. The default method form parameter in Swift is a constant parameter and cannot be modified

Func Test (num1:int) {}

equivalent

Func test (Let Num1:int) {}

If you want the argument to be a variable parameter, you need to manually add the var keyword to the formal parameter name, for example

Func Append (Var deststr:string, sourcestr:string, Num:int), string{for    _ in 0..<num{        deststr + = Source Str    }    return Deststr}var result = Append ("Lifei", "@", 6)

The variable deststr is preceded by a var modifier, so that you can use DESTSTR to perform operations within the method.


5. Input and output parameters, decorated with inout keyword

Func Change (inout num1:int) {    NUM1 = 10}var a = 20change (&a) A
In fact, its principle is to pass the variable's address, not the value. Another example is the exchange of two-digit values.

Func Exchange (InOut num1:int,inout num2:int) {    var temp = NUM1;    NUM1 = num2;    num2 = temp;} var a = 10var B = 20exchange (&a, &b)

Iv. Types of methods

The type of method in Swift is also one of the data types, for example:

Func Test (Num1:int, Num2:int), int{    return NUM1 + num2}

The type of this method is

(int, int), int

You can use a method type to define a variable (the type of the variable fn is the method type of test)

VAR fn: (int, int), int = TESTFN (10,20)

method to use example 1 for the type:

Func Add (num1:int,num2:int)->int{    return num1 + Num2}func minus (num1:int,num2:int)->int{    return NUM1- Num2}func Divide (num1:int,num2:int)->int{    return Num1/num2}func calcresult (fn: (int,int)->int, Num1:int, Num2:int), int{    return fn (NUM1, num2)}var Addresult = Calcresult (add, ten) var minusresult = Calcresult (minus var divideresult = Calcresult (Divide, 100, 5)

As you can see, the type of a parameter in method Calcresult is (int, int), int, and then it is very convenient when called.


method to use Example 2 for the type:

Func gotowork () {    println ("Go to Work")}func Havefun () {    println ("Rest")}func Howtodo (day:int), Void, void{< C2/>if (Day = = 6 |-day = 7) {        return havefun    }else{        return gotowork    }}var fn = Howtodo (1) FN ()

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Swift: Functions and methods

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.