Learn the difference between constant, View, pure in the Solidity syntax of ethereum’s smart contract language.

1

The difference between summary

The function modifiers constant, view, pure in Solidity are used to tell the compiler that the function does not change/read state variables so that the function can execute without consuming gas (at all!). Because you don’t need miners to verify.

Before Solidity V4.17 there was only constant, but it was thought that constant itself represented a constant in variables and was not suitable for functions, so constant was split into View and pure.

  • The view function is the same as constant, you can read the state variable but you can’t change it;

  • Pure is stricter. Functions modified by pure cannot change or read state variables, or they will fail compilation.

You can run the following test code to further understand these three keywords.

contract constantViewPure{

    string name;

    uint public age;

        function constantViewPure() public{

        name = "liushiming";

        age = 29;

    }

        functiongetAgeByConstant() public constant returns(uint){ age += 1; // Declare it constant, and try to change the value of the state variable in the function body. The compiler will report warning, but it can passreturn age;  // return30, but! The value of the state variable age does not change; it is still 29! }functiongetAgeByView() public view returns(uint){ age += 1; // View has the same effect as constant, but can pass warningreturn age; // return30, but! The value of the state variable age does not change; it is still 29! }function getAgeByPure() public pure returns(uint){ 

       returnage; // Compile error! Pure is stricter than constant and View. Pure completely forbids reading and writing state variables!return1; }}Copy the code

2

A detailed description

2.1 Constant status variable

A state variable can be declared as constant. In this case, you can assign values only to expressions whose values are determined at compile time. Any assignment to storage, blockchain data (such as now, this.balance, or block.number) or execution data (msg.gas) or calls to external contracts is disallowed. Expressions with side-effects on memory allocation are allowed, but expressions with side-effects on other memory objects are not. Built-in functions keccak256, SHA256, RIPemd160, ecRecover, addmod and mulmod are allowed (even if they do call external contracts).

The reason for allowing a memory allocator with boundary effects is that it would allow the building of complex objects, such as a look-up table. This feature is not yet fully available.

The compiler does not reserve storage for these variables, and each occurrence is replaced with the corresponding constant expression (which may be evaluated by the optimizer as an actual value).

Not all types of state variables support constant; only value types and strings are currently supported.

Pragma solidity ^ 0.4.0; contract C { uint constant x = 32**22 + 8; string constant text ="abc";

    bytes32 constant myHash = keccak256("abc");

}
Copy the code

2.2 the View function

You can declare a function to be of type View, in which case you must ensure that the state is not changed.

The following statement is considered to be in modified state:

  • Modify state variables.

  • Generate events (solidity – cn. Readthedocs. IO/useful/develop /…

  • Create other contracts (solidity – cn. Readthedocs. IO/useful/develop /…

  • Use selfdestruct.

  • Send ether through a call.

  • Call any function that is not labeled View or Pure.

  • Use low-level calls.

  • Use an inline assembly that contains a specific opcode.

Pragma solidity ^ 0.4.16; contract C {function f(uint a, uint b) public view returns (uint) {

        returna * (b + 42) + now; }}Copy the code

Comments:

  • Onstant is the alias of view.

  • Getter methods are labeled view.

  • The compiler does not enforce that view methods cannot change state.

2.3 Pure function

Functions can be declared as pure, in which case they promise not to read or modify the state.

In addition to the list of state modification statements explained above, the following are considered to be read from the state:

  • Read state variables.

  • Access this.balance or

    The balance.

  • Access any member of block, tx, MSG (except msg.sig and msG. data).

  • Call any function not marked pure.

  • Use an inline assembly that contains some opcodes.

Pragma solidity ^ 0.4.16; contract C {function f(uint a, uint b) public pure returns (uint) {

        returna * (b + 42); }}Copy the code

warning

The compiler does not enforce that the Pure method cannot read state.

Author: HiBlock blockchain community technology evangelist Hui Ge

Originally published in Jane’s book

Shanghai block chain marathon | Blockathon (2018)You’re invited to the roadshow

The following is our community introduction, welcome all kinds of cooperation, exchange, learning 🙂