【試讀版】All In One Solidity
KrypltoCampChiHaoLu
  • Welcome!
  • | Chapter 0 | Intro
    • 【推薦序】
    • 【前言】
  • Part I Basic
    • | Chapter 1 | 基本介紹 Introduction
      • 【環境建置 Remix IDE】
      • 【版本控制】
      • 【Hello World & First Contract】
      • 【Practice】
      • 【Answer】
    • | Chapter 2 | 型別 Types & 變數 Variables
      • 【Integer】
      • 【Bool】
      • 【Address】
      • 【Contract & This】
      • 【String】
      • 【Scope】
      • 【Practice】
      • 【Answer】
    • | Chapter 3 | 單位 Unit & 運算子 Operators
      • 【單位】
      • 【Time】
      • 【運算子】
      • 【Practice】
      • 【Answer】
    • | Chapter 4 | 流程控制 Selection and Repetition
      • 【If-Else】
      • 【For】
      • 【While】
      • 【Practice】
      • 【Answer】
    • | Chapter 5 | 函式 Function
      • 【Returns】
      • 【Visibility】
      • 【stateMutability】
      • 【Constructor】
      • 【Function Overloading】
      • 【Fallback】
      • 【Useful Function】
      • 【Practice】
      • 【Answer】
    • | Chapter 6 | 資料結構 Data Structures
      • 【Array】
      • 【Mapping】
      • 【Structs】
      • 【Enum】
      • 【Practice】
      • 【Answer】
    • | Chapter 7 | 角色和全局訊息 Global Variables
      • 【Msg】
      • 【Block】
      • 【ABI】
      • 【TX】
      • 【Practice】
      • 【Answer】
  • Part II Medium
    • | Chapter 8 | 記憶體配置 Memory Allocation
    • | Chapter 9 | 異常處理 Handling Exception
      • 【Require】
      • 【Assert】
      • 【Revert】
      • 【Try/Catch】
      • 【Practice】
      • 【Answer】
    • | Chapter 10 | 事件 Event
      • 【emit】
      • 【Indexed】
      • 【web3.eth.subscribe()】
      • 【Logs with Data & Topics】
      • 【anonymous】
      • 【Practice】
      • 【Answer】
    • | Chapter 11 | 繼承 Inheritance
      • 【Inheritance】
      • 【Modifier】
      • 【合約互動】
      • 【Function Overriding】
      • 【Polymorphism】
      • 【多重繼承與 super】
      • 【Practice】
      • 【Answer】
    • | Chapter 12 | 介面 Interface
    • | Chapter 13 | 引用 Imports & 函式庫 Libraries
      • 【Library】
      • 【Import】
      • 【OpenZeppelin】
      • 【Practice】
      • 【Answer】
    • | Chapter 14 | ERC & Token
  • PART III Advanced
    • | Chapter 15 | 佈署 Deploy & 編譯 Compiler
    • | Chapter 16 | 開發工具 Dev. Tools
    • | Chapter 17 | 最佳化合約 Contract Optimization
  • | OTHERS | Information & Reference
    • 【結語】
    • 【參考資料】
Powered by GitBook
On this page

Was this helpful?

  1. Part I Basic
  2. | Chapter 5 | 函式 Function

【stateMutability】

除了以上的可視性之外,我們還可以增加一些函數的敘述來定義這個函數的類型。

function myFunction() <visibility specifier> <stateMutability> returns (bool) {
    return true;
}
  • pure : 不允許修改或訪問

  • view : 不允許修改狀態變量

  • payable : 允許函數在調用同時接收 Ether,只有宣告 payable 的物件才可以進行交易

View vs. Pure

  • 在早些版本他們被稱為constant函數

  • View Function: 可閱讀但不可更改任何狀態變數

  • Pure Function: 不可閱讀也不可更改任何狀態變數

Pure Function

  • 可以呼叫其他的Pure function

  • 不可以呼叫View function,也不可以呼叫其他的沒有標明Pure或View,但卻會更改到狀態的函數(也被稱作Writing function)

  • 不可以讀取或對狀態變數做任何更改

View Function

  • 可以存取任何的狀態變數,但僅能做讀取用,不可進行修改。

  • 可以呼叫其他的Pure function

  • 不可以呼叫其他的沒有標明pure或view,會更改到狀態的函數(也被稱作Writing function)

Previous【Visibility】Next【Constructor】

Last updated 3 years ago

Was this helpful?