> For the complete documentation index, see [llms.txt](https://guanming.gitbook.io/zgg-doc/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://guanming.gitbook.io/zgg-doc/zgg-ji-ben-yu-fa/function.md).

# ZGG的函数

## 函数定义

### 命名函数

```
func 函数名(参数表) {
    // 函数体
}
```

### 匿名函数

```
// 普通函数
xxx := func(参数表) {
    // 函数体
}

// 箭头函数
yyy := (参数表) => 返回值表达式
zzz := (参数表) => {
    // 函数体
}

// 如果箭头函数只有一个参数，括号可以省略
aaa := onlyArg => {...}
bbb := onlyArg => expr

// 如果将匿名函数作为参数传递另一个函数调用，还有一种简化写法。以下两种写法等价
someOtherFunc({ 函数体 }) 
someOtherFunc(it => { 函数体 }) 
```

### 参数表的定义

* 普通参数表

略

* 可变参数表

最后一个参数前面如果加上...，则代表其为一个可变参数

#### Examples:

```
zgg> f := (a, b, ...c) => [a, b, c]
<anonymous function>

zgg> f(1)
[1, undefined, []]

zgg> f(1, 2)
[1, 2, []]

zgg> f(1, 2, 3)
[1, 2, [3]]

zgg> f(1, 2, 3, 4)
[1, 2, [3, 4]]
```

## 函数调用

基本调用形式为：

```
函数标识符(参数表)
```

#### Examples:

```
// 基本的例子见上文可变参数定义段落

// 可以使用...array，将一个数组展开，作为参数表的一部分
zgg> args := [1, 2, 3]
zgg> f(100, ...args, 5) // 相当于f(100, 1, 2, 3, 5)。f定义见“可变参数”段落
[100, 1, [2, 3, 5]]
```
