iOS Development swift-functions

Source: Internet
Author: User
Tags types of functions

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

Func function name (formal parameter list), Void {    //  function Body ...   () {    //  function Body ...  //  function body ...}
View Code

(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 ... 3}

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

Func Find (ID: Int)(name:string, age:int) {if ID>0 {        return("Jack", -)    } Else {        return("Nobody",0)}}var People= Find (2) println ("name=\ (People.name), age=\ (people.age)")
View Code

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 ... 3}

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

Func Append (varstring: String, suffix:string, Count:int)string{ for_inch 0.. <Count {string+=suffix}return string}append ("Jack",".",4)//calling Functions//Jack .... .
View Code

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

func Swap (inout num1:int, inout num2:int) {    = num1    = num2    =20 The Swap (///  parameters must precede the &//    The value of a is the value of 10,b
View Code

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)

Vi. Types of functions

A function type is also a type of data that consists of a formal parameter type and a return value type, in the form

return value type (List of parameter types)

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

The function type of the SUM function is (int, int), int

1 func printline () 2 {3     println ("-----------") 4}

There are 4 ways to represent the function type of the PrintLine function

(1) void, void

(2) () ()

(3) Void-and ()

(4) () Void

Defining variables using function types

You can use function types to define variables, and you can store this type of function in the future.

1 func sum (num1:int, num2:int), int {2     return NUM1 + num23}4 var fn: (int, int), int = SUM5 fn (10, 20)// Returns 30

Because Swift has a type inference mechanism, it is also possible to write this

var fn = sum//FN After the type of the stored function must be (int, int), int

functions as parameters

As with other data types, functions can also function as arguments

1 func printresult (fn: (int, int), int, num1:int, Num2:int) 2 {3     println ("Operation Result:%d", FN (NUM1, num2)) 4}

The FN parameter receives a function that must return an int, with 2 arguments of type int

Func sum (num1:int, num2:int), int {    return num1 +, int {    retur n num1-ten/   /// Ten
View Code

Nine, function as return value

As with other data types, functions can also function as return values

Func gotowork () {println ("go to work")}func Playfootball () {println ("play football")}func Howtodo (day:int)(), () {    ifDay <6 {        returnGotowork}Else {        returnPlayfootball}} VAR fn= Howtodo (7) fn ()//play football
View Code

X. Function overloading

Function overloading: Function name is the same, function type is different

The following 2 functions form an overload

(1) Function name: Sum, Function type: (int, int), int

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

(2) Function name: Sum, Function type: (int, int, int), int

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

Xi. nested functions

Global functions: Functions defined in the global scope

Nested functions: Functions defined in the body of a function

code example:

1 func Howtodo (Day:int), () {2     func gotowork () {println ("Go To Work")} 3     func Playfootball () {println ( "Play Football")} 4     If day < 6 {5         return gotowork 6     

Note: line 10th is the wrong wording, and the scope of the nested function is limited to the inner body of the function that defines the nested function

iOS Development swift-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.