Use the func keyword in 1.Swfit syntax to declare a function
func Sayhi () { println ("Hi! " )}
The results show: Hi!
2. function with parameter and return value function, parameter string, return value string
Func Sayhitoperson (personname:string),string{ return" " + PersonName;} println (Sayhitoperson ("Girl"))
The results show: Hi! Girl
3. External parameter name, when used, there will be a corresponding hint, if the external parameter name is the same as the local parameter, just add a # to it.
Func area (w a:double, H b:double), double{ return A *3.03.0 /c8>-> double{ return A *3.03.0))
Results showed: 9.0
Results showed: 9.0
4. Variable parameter, variable parameter means that if the parameter is more, you can use " type ..." to represent multiple parameters
A. A function can accept only one variable parameter
B. A mutable parameter must be at the end of the function
Func sum (X:int ...), int{ 0for in x{ t+ =V } return t}println (SUM (1,2,3,4,5,6))
Results showed: 21
5. The default parameter is that when the parameter is not filled in, the parameter is passed to a value with a default parameter of TA
" Lucy " , what:string) { println ("\ (WHO) say \ [what]" "Tom" "Hello")
Results show: Tom say Hello
6. Variable parameters, the default parameter is constant, here you can also define a variable parameter
Func FAC (var x:int)- int{ 1while 1{ s*=x-- } return s}println (FAC (5))
Results showed: 120
7.in-out parameters, to exchange two numbers
func Swap (inout a:int, inout b:int) { = a^b; = a^b; = a^13swap (&a,&b)// incoming actual address of a and B println (" results after Exchange: A=\ (a) b=\ (b)")
The result shows: after the exchange the result is: A=3 b=1
8. Tuple types, functions that do not return a value always return void, in Swift, void represents a tuple, and no return value is an empty tuple
Func areawithcircum (#width: int, #height: int), (int, int) { return (Width*height, width+ Height) <<144))
The results show: (+)
9. Function type, after the colon is a function (double, double), double
A. Function type can define a constant or variable
var Areafunc: (Double, double), double = areaprintln (areafunc (3.0,3.0))
Results showed: 9.0
B. Function types can be used as parameter types
Func Printlnarea (area: (double,double)-double,width:double,height:double) { println (" \ (Area (width, height)) " )}printlnarea (Areafunc,3,3)
Results showed: 9.0
C. The function type is returned as a return type because Getmathfunc returns a function ... The parameter of this function is int again, so we need to pass in a parameter ...
Of course it can be written as Getmathfunc (type: "Squre") (10)
Func squre (A:int)int{returnA *A}func Cube (a:int)-int{returna*a*A}func Getmathfunc (#type: String)(INT)int{ifType = ="squre"{ returnSqure}Else{ returnCube}} var mathfunc= Getmathfunc (Type:"squre") println (Mathfunc (Ten))
Results showed: 100
10. Function nesting, function nesting is a function that contains another function
Func Getarea (type:string), (INT)int{func squre (a:int)-int{returnAA} func cube (a:int)-int{returnA * A *A}Switchtype{ Case "squre": returnsquredefault: returnCube}} var getareainstance= Getarea ("squre") println (Getareainstance (4))
Results showed: 16
The Squre function in Getarea will be destroyed by leaving Getarea, so the function names of squre and cube do not have any meaning! So you can use closures to simplify the notation of nested functions ~
Closed-Packet expression
//{
(formal parameter list), return value type in
An executable expression
//}
func getArea1 (type:string)-(Int)-> int{ switch type{ case " squre " : {(int) , int in return a * A} default : return {(int) , int in retu RN A * a * A}}}
Careful observation of the above code, in fact, " {xxx in XXX} " form, the previous xxx is " parameter list and return type ", followed by xxx is the expression ...
In addition, the ~swift closure can be analyzed by the context of the parameter list, the parameter list can be used $0$1 .... $n this way. So the new writing came up again ~
var Result:int = { 1for in 1... $1{ *= $0 } return result} (34 ) println (Result)
Results showed: 81
11. Trailing closure, refers to the function's parameter type is the closure type
Func someFunction (Num:int, fn: (int), ( int)) { // Execute code println (num * FN ( Ten ) )}somefunction ($0 * $0 })
Results showed: 2000
Capture variables or constants in context, in short, the closure of this thing is very flexible! You can figure it out slowly.
Func Makearr (ele:string),[ string]{= [] , [string]{ arr.append ( ele) return arr } return addelement}println (Makearr ( " Lucy ") ())
Results show: [Lucy]
Swift functions and closures