Solidity: What is the difference between `view` and `constant`?

  • Solidity 0.4.16 introduced the view and constant function modifiers. The documentation says:

    constant for functions: Same as view.

    Does this mean view is just an alias for constant? If so, why do we need it?

    This answer explains the meanings of `constant`, `pure` , and `view` very well.

  • benjaminion

    benjaminion Correct answer

    4 years ago

    This is discussed here: and has been partially implemented in the change you noted.

    As I understand it, the point is that constant is misleading (constant functions don't necessarily return constant results), and not particularly nuanced. The changes when complete will introduce two replacement keywords: view and pure which are intended to be more meaningful and useful.

    From the GitHub issue,


    • constant function should not modify the state (not fully enforced yet)
    • constant state variable (ie. the one in the class and not in a method) is evaluated every time it is called

    After the change:

    1. the keyword view is introduced for functions (it replaces constant). Calling a view cannot alter the behaviour of future interactions with any contract. This means such functions cannot use SSTORE, cannot send or receive ether and can only call other view or pure functions.
    2. the keyword pure is introduced for functions, they are view functions with the additional restriction that their value only depends on the function arguments. This means they cannot use SSTORE, SLOAD, cannot send or receive ether, cannot use msg or block and can only call other pure functions.
    3. the keyword constant is invalid on functions
    4. the keyword constant on any variable means it cannot be modified (and could be placed into memory or bytecode by the optimiser)

License under CC-BY-SA with attribution

Content dated before 7/24/2021 11:53 AM