Swift's function 脚本语言
has many similarities. If one day with the swift Development server, look forward to wow (everything is possible, after all, they say to run on Linux),??
Judging by the number of arguments
No parameters
Func MyFunc ()-int{}
Single parameter
Func MyFunc (first:int),int{}
Multi-parameter
Func MyFunc (First:int, Other:int),int{}
Judging from the return value
No return value
Func MyFunc () {}
Single return value
Func MyFunc ()-bool{}
Multi-value return
Func MyFunc () (Min:int, Max:int)
元组的成员不需要在函数中返回时命名,因为它们的名字已经在函数返回类型中有了定义
var ret = MyFunc () print ("min:\ (ret.min), max:\ (Ret.max)")
Judging by the parameter name
The function arguments have an external parameter name (external parameter name) and a local parameter name (locally parameter name).
The first parameter omits its external parameter name, and the second argument uses its local parameter name as its own external argument name. All parameters need to have different local parameter names, but 可以共享相同的外部参数名
.
func MyFunc (first:int, first a:int, first b:int) {} // but is it really good to write an external parameter name like this? MyFunc (13);
The use of an external function name allows the function to be expressed in a sentence, and makes the function body readable and can express the explicit intent of the function.
Ignore external parameter names
If this is the case:
Func MyFunc (First:int, a:int),int{ return}myfunc ( 1//
So how do you implement calling myFunc(12,1)
such a function call directly?
Func MyFunc (First:int, _ A:int),int{ return}myfunc ( 1// OK
Default parameters
) int{ return parameter}myfunc () //
Place the parameter with the default value in the 函数参数列表的最后
. This guarantees that the order of the non-default arguments is consistent when the function is called, and makes the same function clearer when invoked under different circumstances.
Variable parameters
可变参数
can accept 0 or more values
Func MyFunc (par:double ...) -double{ 0for in par { + = number } return total}myfunc (1,2,3,4,5 ) MyFunc (1,5,8.9)
一个函数最多只能有一个可变参数
If the function has one or more parameters with default values, and there is a mutable parameter, then the variable parameter is placed at the end of the parameter table.
Func MyFunc (a:double =Ten, par:double ...) -double{var total:double=a forNumberinchpar { total+=Number }returntotal}myfunc (par:1,2,3,4)// -MyFunc ( -, Par:1,2,3,4)// -
Variable parameters
Is that the value of this parameter can be changed in the body of the function, by default we all know that the parameter value is a let
" Hello, playground. " func MyFunc (var str:string), string{ "@"+str return str}myfunc (str)
Modifications to variable parameters disappear after the function call ends and are not visible outside the function body. Variable parameters exist only in the life cycle of a function call.
Input and OUTPUT parameters
The mutable arguments above are only mutable in the body of the function, but how can a function modify the values of the parameters and want them to persist after the function call ends. Then there is the In-out parameter, which is a bit like a C + + reference.
You can only use variables as 输入输出参数
. You cannot pass 常量或者字面量
in (literal value), because these quantities are 不能被修改
. When an incoming parameter is used as an input and output parameter, it needs to be added in front of the parameter to &符
indicate that the value can be modified by the function.
Input and output parameters 不能有默认值
, and 可变参数不能用 inout 标记
. If you mark a parameter with inout, this parameter cannot be marked by Var or let.
func Swap (InOut a:int, InOut _ B:int) { = a = b =3 not let a = 3107//Swap (3, 107) ErrorSwap (&a, &b)
function type (functions Types)
()---Void (int, int)--(int, int)//...
function types are used the same way as normal types, either as parameter types or as return types
Functions can be nested
Func MyFunc (A:int, B:int)--int{ func Addab (A:int, _ B:int),int{ return A + b; } Func Retfunc (a:int),int{ return Addab (A, b) * b } return Retfunc} (MyFunc (34)) (3)
For functions, there may be other things, such as custom operators, overloads, Generics, and a piece (which I've also been interested in, as I've been 函数式编程
writing about earlier). This kind of knowledge I will slowly fill up, haha, we study together, the problem of the place please help me point out.
Swift's function