DEV Community

Cover image for Ethereum-Solidity Quiz Q18: What type of modifiers are "view" and "pure"?
MihaiHng
MihaiHng

Posted on

Ethereum-Solidity Quiz Q18: What type of modifiers are "view" and "pure"?

They are not visibility modifiers, they're state mutability modifiers.

State Mutability Modifiers describe whether a function reads or modifies state.

"view"

  • Can read state variables
  • Cannot modify state
  • Cannot emit events (technically they can in newer versions, but it's not very common)
  • Does not consume gas (when called externally)
  • Must return a value

"pure"

  • Cannot read state variables
  • Cannot modify state
  • Cannot emit events
  • Does not consume gas (when called externally)
  • Can only use function parameters and local variables
  • Must return a value

Default (no modifier)

  • Can read state
  • Can modify state
  • Can emit events
  • Costs gas to execute
  • State-changing function
Aspect Default view pure
Read state
Modify state
Emit events
Costs gas ❌ (external) ❌ (external)
Use parameters

Top comments (0)