Functions & Closures

Functions

1、不带返回值的函数:

  Functions & Closures

2、通过tuple返回元素

  Functions & Closures

  返回的tuple可按如下方式使用:

  Functions & Closures

3、External Parameter:

  Functions & Closures

  External parameter的使用:

    

4、Shorthand external parameter:

  Functions & Closures

5、Default Parameter:

  Functions & Closures

  Default Parameter自动定义了external parameter:

  Functions & Closures

  Functions & Closures

6、Variadic Parameters,变长参数,在类型后面加上...:

  Functions & Closures

7、Variable Parameter,函数参数默认是const,即函数body内无法改变参数值。要想在函数body内改变此值,需用var声明参数。

  Functions & Closures

8、In-out参数:

  Functions & Closures

  使用in-out参数:

  Functions & Closures

9、FunctionType:

  

  可以在String Interpolation中调用函数:

  

10、FuntionType as Parameter Type:

  Functions & Closures

11、FunctionType as Return Type:

  Functions & Closures

Closures

1、三种Closure:

  Functions & Closures

2、Closure Expression Syntax:

  

  Functions & Closures

3、Inferring Type From Context

  

  在单表达式Closure的情况下,return也可以省略:

  

4、Shorthand Argument Names,通过数字

  

5、Operator Function

  

6、Trailing Closure

  

  array.map使用Trailing Closure的例子:

  Functions & Closures

7、下述代码中,runningTotal会capture reference:

  Functions & Closures

8、Closure are reference types。

你可能感兴趣的:(functions)