> For the complete documentation index, see [llms.txt](https://chihaolu.gitbook.io/all-in-one-solidity/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://chihaolu.gitbook.io/all-in-one-solidity/part-i-basic/chapter-2-xing-bie-types-bian-shu-variables/scope.md).

# 【Scope】

Scope 被翻譯為作用域，規範著變數的生命週期與記憶體配置方式。

* Local
  * 如果宣告在函式內則為範圍變數
  * 並不是儲存在區塊鏈上
* State
  * 如果宣告在函式外則為狀態變數
  * 儲存在區塊鏈上
  * 預設為 `private`
* Global
  * 區塊鏈提供的訊息，又稱為全局訊息，之後會有專門的篇章講解

#### State variables

狀態變數從我們執行它開始它就會一直存在，也就是說它們是永遠存在區塊鏈上的，所以如果要改他們就要花錢。然而 Local variable 只有在執行函數時才會存在。

狀態變數的 `string` 和 `values type` 還可以加上以下兩者 `modifier`：

* `constant`: 宣告了 `constant` 的變數在編譯後就不可以再被更改，不會佔據Storage Slot。
* `immutable`: 宣告了 `immutable` 的變數可以在建構子（`constructor`）時被修改，在此之後就不可以被更改。建構子的部分之後會講解。
