swift--function

Source: Internet
Author: User

1Func SayHello (personname:string)string//Return is a string type2 {3Let greeting ="Hello"+ PersonName +"!"4        returnGreeting5 }6 7println (SayHello ("Lin"))//hello lin!8 9Func Add (A:int, B:int),IntTen { One        returnA +b A } -  -println (Add ( -, -))//50 the  -Func process ()Float - { -        return 3* -; + } -  + println (Process ())//60 A  at Func method () - { -println"Hello World") - } - method ()//hello World -  in //returns more than one value -Func count (string: String),(Vowels:int, Consonants:int, Others:int) to { +         varvowels =0; -         varconsonants =0; the         varothers =0; *          forCinch string $         {Panax Notoginseng                SwitchString (c). lowercasestring -                { the                         Case "a","e","I","o","u": +++vowels; Cannot have space behind + + A                         Case "b","C","D","F","g","h","J","k","L","m","N","P","Q","R","s","T","v","W","x","y","Z": the++consonants +                        default: -++others $                 } $          } -          return(vowels, consonants,others) - } the  -Let total = count ("some arbitrary string!")Wuyiprintln"\ (total.vowels) vowels and \ (total.consonants) consonants")//6 vowels and consonants

1. Extended parameter name

1Func process (p1:string, p2:int)String2 {3         return "Name:"+ p1 +"Age :"+String (p2)4 }5println (Process ("Bill", -))//name:bill age:206 7Func Process1 (name p1:string, age p2:int)String8 {9          return "Name:"+ p1 +"Age :"+String (p2)Ten } Oneprintln (PROCESS1 (name:"Mike", Age: -))//name:mike age:17.
println (Process1 (age:17,name: "Mike")//This will be an error, said no name found, so this order must be strictly consistent.

2, internal parameter name and extension parameter name merged

1 func process2 (#name: String, #age: Int),string2{3      return  'name: ' ' Age :' + String (age)   4 }5 println (PROCESS2 (name:"BILL", Age:))

3. Default parameter values

1Func PROCESS3 (Name p1:string ="MIKE", Age P2:int = -),String2 {3Reutrn"Name:"+ p1 +"Age :"+String (p2)4 }5 6 println (PROCESS3 ())7println (PROCESS3 (name:"JOHN"))8 9Func process4 (name:string ="Mike", Age:int = -),StringTen { One        return "Name:"+ name +"Age :"+String (age) A } -  -println (PROCESS4 (name:"..."))//Although the preceding is defined with the internal parameter name, but because of the subsequent assignment operation, so the intrinsic parameters are implicitly converted to external parameters, if not add Name: will be an error. 

4. Variable parameters

5. Constants and Variable parameters

6. Input and OUTPUT parameters

7. Function type

8. Nested functions

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