# 變數

變數可以是任意基本資料型別，也可以是任意自定型別。變數的型別由其識別符號後面的特殊字元決定。

### 變數型別 <a href="#variable-types" id="variable-types"></a>

這些特殊字元被稱為**型別標籤**，其中包括：

* `%` = 整數值變數
* `#` = 浮點值變數
* `$` = 字串變數
* `.{型別名}` = 自定型別變數

以下是有效變數的示例：

* `Score%`
* `speed#`
* `name$`
* `player.Player`

型別標籤只需要在定義（第一次呼叫時）新增即可。在此之後，您可以選擇性忽略掉型別標籤。

如果在定義時沒有指明型別標籤，則變數將預設認為是整數值變數。

將同一變數名用於定義不同型別的變數是非法的。例如，如果您已定義了一個叫做 `name%` 的整數值變數，那麼再定義一個叫做 `name$` 的字串變數就是非法的。

### 變數賦值 <a href="#setting-variables" id="setting-variables"></a>

Blitz 使用 `=` 關鍵字為變數賦值。例如：`score% = 0` 將為整數值變數 `score` 賦值 `0`。

### 變數作用域 <a href="#variable-scope" id="variable-scope"></a>

變數可以是全域性變數，也可以是本地變數。這定義了程式中可以使用該變數的位置。

* **全域性變數**可以在程式的任意位置使用。
* **本地變數**只能在建立變數的函式內使用。

`Global` 關鍵字被用於定義一個或多個全域性變數。例如：

```basic
Global Score = 0, Lives = 3, Player_up = 1
```

將定義三個全域性變數。

相似地，`Local` 被用於定義本地變數：

```basic
Local temp_x = x, temp_y = y
```

如果變數在定義時未指明是全域性變數還是本地變數，則變數預設為本地變數。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gitbook.ziyuesinicization.site/blitz-basic-language-reference/zh_hant/variables.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
