This chapter looks at Swift's function declaration and invocation
Function declaration func function name (parameter), return type {function code block}
Let's make a statement.
//no parameter no inversefunc Test () {print ("Asdfa");}//There are arguments and anti-Func test1 (A:int)int{return++A}//There is no counter-argumentfunc test2 (a:int) {print (a)}func test (A:int)-void{Print (a);}//non-parametric and anti-Func Test ()int{return 1}
The basic four types of functions are like this, very simple
Take a look at Swift's special place
External name
When the SWIFT function has multiple arguments, the wording differs from other languages
Func test5 (A:int, B:int),Int { return +b}test5 (53)
notice what's different when you call here? There's a B: Yes, that's the external name . The external name is mutable, for the caller you can change B to s or any variable name (conforming to the variable naming convention)
Func Test5 (A:int, S b:int),Int { return +b}test5 (53)
This will change the external name to the first variable is not the external name, of course, you can also give him a just like the variable name of B changed to s like
Default value
This is the literal meaning that the parameter has a default value that is set when the function is declared
5 5)Int { return +B}
Test6 () Test6 (53) Test6 (53)
If the function parameter declares a default value then there are so many ways to call it (the parameter that is not given will be used by default)
Here are some points to note
1. With default values, all parameters are left behind
2. Call the parameter order as much as possible in the order in which the function is declared
Modified
By default, the parameter type is a let constant, and if you need to modify it, add Var
Add inout If a reference value is required
Fun test7 (a:int) { //a=3 (Error) Print (a) }fun Test8 (var a:int) { A =3 Print (a) }fun test8 (inout a:int) { a=3 print (a ) 5Test8 (a) a
Variable parameter length
Func Add (Initnum:int,numbers:int ...) -Int { = initnum for in Numbers { + = num } return Totals}add (56) Add (6, Numbers:2,4, 3)
As the second argument, no matter how many int type parameters are set up as numbers arrays
Introduction to Swift functions