Functions in Swift (top)

Source: Internet
Author: User

Learning from the Geek Academy: a dictionary in Swift

Tool: Xcode6.4

Directly on the basis of the sample code, more knocks more experience will have a harvest: hundred see not like a knock, a knock will

1 Import Foundation2 3 //function4 /*1. Defining the syntax format of a function5 func function name (formal parameter list), return value type {6 functions composed of//executable statements7 }8 */9 //defining and calling functionsTenFunc SayHello (personname:string)string{ One     return "Hello"+PersonName A } -println (SayHello ("Heyang")) -  the //2. External parameter name local parameter name: formal parameter type -Func area (w width:double, H height:double)Double - { -     returnWidth *Height + } -Print"the area is:") +println (Area (width:3.4High:4.3)) A //but it's more troublesome, so you need a # # atFunc area_1 (#width: Double, #height: Double),Double - { -     returnWidth *Height - } -Print"after the change:") -println (area_1 (width:34.1, Height:9.2)) in  - //3 variable parameter: Add after parameter type ... Represents the parameter toFunc sum (Numbers:int ...)int{ +var total:int =0 -      forNuminchnumbers{ theTotal + =Num *     } $     return TotalPanax Notoginseng } -println"sum is: \ (sum (1,2,3,4))") the //Note: A function can have only one variable parameter, and the variable parameter can only be in the last +  A //4. Default Parameters theFunc Sayhi (msg:string,name:string ="Heyang"){ +println"\ (name), \ (msg)") - } $Sayhi ("Hello ~") $Sayhi ("Hello ~", Name:" World") - //You cannot remove the name from the inside: that is, with the default parameter is the external parameter name, that is, the default parameter equivalent is defined in the external declaration. If you want to ignore it, add an underscore before the default parameter: UNC sayhi (msg:string,_ name:string = "Heyang") is not _name but _ name -  the //If there are variable parameters, there are default parameters, then the variable parameter is placed on the last side, the default parameter can only appear after the second - Wuyi //5. Constant parameters and variable parameters theFunc factorial (var number:int)int{ -var result:int =1 Wu      whileNumber >1{ -result = result* Number Aboutnumber-- $     } -     returnresult -     //the parameter scope of the variable is inside the function, so how the parameter changes will not affect the outside variables - } APrint"Formal Parameters") +println (Factorial (4)) the  - //6.in-out Formal Parameters $ func swap (inout a:int,inout b:int) { thevar temp =a theA =b theb =Temp the } -var a =1 invar B =2 theSwap (&a, &b) theprintln"after the exchange, A:\ (a) b:\ (b)") About /*Note the 1. You can only pass in a variable as an argument the 2. Input and output parameters cannot have default values the 3. If you use a keyword, inout marks a parameter that cannot be marked with VAR or let + */

The results of the run output are:

Functions in Swift (top)

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.