iOS Development Swift Chapter-(vii) function

Source: Internet
Author: User

iOS Development Swift Chapter-(vii) function

Definition of a function

(1) The definition format of the function

1 func function name (formal parameter list)--return value type  {2     //function body ... 3 4}

(2) format of formal parameter list

Formal parameter name 1: Parameter type 1, parameter name 2: Parameter Type 2, ...

(3) Example: Calculates the number of 2 integers and

1 func sum (num1:int, num2:int), Int {2     return NUM1 + num23}

(4) function with no return value

If the function does not return a value, there are 3 ways to

1 func function name (formal parameter list), Void {2     ///function body ... 3} 4  5 func function name (formal parameter list), {6     //function body ... 7} 8  9 func function name (formal parameter list) {Ten  //function body ... 11}

(5) functions with no formal parameters

If a function has no formal parameters, the parentheses after the function name cannot be omitted.

1 func function name (), Int {2     //function body ... 9 ·

The above function has no formal parameter, the return value is int type

(6) functions that return tuples

A function can also return the tuple data

1 func Find (Id:int)-(name:string, Age:int) {2     if ID > 0 {3         return ("Jack", "") 4     } else {5         Retu RN ("Nobody", 0) 6     }7}8 var people = find (2) 9 println ("Name=\ (People.name), age=\ (people.age)")

Second, external parameter name

1. Brief description

In general, the meaning and function of this parameter can be inferred by the name of the formal parameter.

1 func addstudent (name:string, Age:int, No:int) {2     println ("Add Student: name=\ (name), age=\ (age), no=\ (No)") 3}

The function of these 3 parameters is known within the functions of the parameter name.

However, the formal parameter is used inside the function, and when the function is called the name of the parameter is not visible, it is possible that the meaning of each parameter will not be understood later

For Addstudent ("Jack", 20, 19) A glance, can guess the 1th parameter "Jack" refers to the name, the following 20, 19 what is the meaning of each?

To address these issues, Swift provides external parameter name syntax

An external parameter name reminds you of the meaning of each parameter when calling a function

2. Definition

The name of the external parameter is defined in the following format:

1 func function name (external parameter name parameter name: formal parameter type), return value type  {2       //function body ... 9 ·

The external parameter name is written in front of the formal parameter name, separated by a space from the formal argument name

3. Use of external parameter names

1 func sum (number1 num1:int, number2 num2:int), Int2 {3     return num1 + num24}5 sum (number1:10, number2:20)// Calling functions

Note: Once you have defined an external parameter name, you must add the external argument name when calling the function

4. Abbreviations for external parameter names

Use # to simplify the definition of external parameter names

1 func sum (#num1: int, #num2: int) 2 {3     return NUM1 + Num24}

The 1th line of code means: NUM1, num2 is both the formal parameter name and the external parameter name

Call function sum (num1:10, num2:20)

Third, default parameter value

(1) When defining a function, you can specify a default value for the parameter, and when the function is called, it is not possible to pass a value to the parameter

1 func addstudent (name:string, age:int =) {2     println ("Add 1 Students: name=\ (name), age=\ (age)") 3}

Addstudent ("Jack")

The age parameter has a default value of 20, so the 4th line can call the function without passing the value to the time parameter

The output is:

Added 1 students:name=jack,age=20

(2) Parameters with default parameter values, Swift automatically generates an external parameter name that is the same as the formal parameter name

Age:int = 20 equals #age:int = 20

Therefore, if you want to pass in the age parameter value, you can only call

Addstudent ("Jack", age:25)

(3) Add an underscore to the parameter name with default parameter values, and you do not have to write the external parameter name when calling the function

1 func addstudent (name:string, _ Age:int =) {2     println ("Add 1 Students: name=\ (name), age=\ (age)") 3}4 5 addstudent ("Jack", 25)

Iv. Constants and Variable parameters

By default, the parameters of the function are constant parameters and cannot be modified inside the function

1 func Test (num:int) {2     num = 103}4 5 func Test (num:int) parameter equivalent to func test (let Num:int)

Note: The 2nd line of code will error

In some cases, you might need to modify the value of a parameter inside a function, you need to define a variable parameter

Add a var in front of the parameter name

1 func Test (var num:int) {2     num =  3}

Write a function to stitch n other strings at the end of a string

1 func append (Var string:string, suffix:string, Count:int), String2 {3 for     _ in 0..<count {4         string + = SUFFIX5     }6     return string7}8 append ("Jack", ".", 4)//Call Function 9//Jack ....

Five, input and output parameters

1. What are input and output parameters?

In the C language, you can use pointers to modify the values of external variables inside a function

In Swift, the value of an external variable can also be modified inside a function by using an input-output parameter

Input and output parameters, as the name implies, can enter a value to come in, you can also output a value to the external

2. Definition of input and output parameters

Add a inout keyword in front of the parameter name to

Func swap (inout num1:int, inout num2:int) {

}

3. Code example: Write a function to swap the values of the external 2 variables

1 func swap (inout num1:int, InOut num2:int) {2 let     tempNum1 = num13     num1 = num24     num2 = TempNum15}6 var a = 207 var B = 108 Swap (&a, &b)//The passed parameter must be preceded by &9//after the swap function is executed, the value of a is 10,b value is 20

4. Use of input and output parameters note

When you pass an argument, you must precede the argument with &

You cannot pass in constants or literals (such as 10) as parameter values (because they are not modified)

Input and output parameters cannot have default values

Input/output parameter cannot be a variable parameter

The input and output parameters can no longer be used with Let, var modifiers (inout-and-let, var cannot coexist)

One of the values of the input and output parameters

You can implement multiple return values for a function (in fact, let the function return a tuple type, you can also implement the return of multiple values)

iOS Development Swift Chapter-(vii) function

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.