iOS Development Swift Chapter-(eight) functions (2)

Source: Internet
Author: User

iOS Development Swift Chapter-(eight) functions (2)

First, the function type

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

Ii. 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

Third, function as parameter

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

1 func sum (num1:int, num2:int), int {2     return NUM1 + num23}4 func minus (Num1:int, num2:int), int {5
    
     return num1-num26}7 printresult (sum, all, ten)//308 Printresult (minus, 20, 10)//10
    

Iv. functions as return values

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

1 func gotowork () {println ("Go To Work")} 2 func Playfootball () {println ("Kick Soccer")} 3 func Howtodo (Day:int)--() { 4     If day < 6 {5         return gotowork 6     } else {7         return Playfootball 8     } 9}10 var fn = Howtodo (7) 11 FN () 12//Play football

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

Six, 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 Chapter-(eight) functions (2)

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.