ReasonJun

Solidity : Modifier (pure, view) 본문

Blockchain/Solidity

Solidity : Modifier (pure, view)

ReasonJun 2023. 10. 16. 00:30
728x90

Pure and view functions are special types of functions in Solidity that have different restrictions on how they can interact with the state of the contract.

 

Pure functions do not read or modify the state of the contract. They can only use the parameters that are passed to them and local variables. Pure functions are often used to perform calculations or to validate data.

 

View functions can read the state of the contract, but they cannot modify it. View functions are often used to retrieve data from the contract or to perform calculations on the data.

 

Both pure and view functions can be called by anyone, inside or outside of the contract.

Here is an example of a pure function:

function add(uint256 a, uint256 b) public pure returns (uint256) {
    return a + b;
}

This function simply adds the two parameters together and returns the result. It does not read or modify the state of the contract.

 

Here is an example of a view function:

function getBalance() public view returns (uint256) {
    return address(this).balance;
}

This function returns the balance of the contract. It reads the state of the contract, but it does not modify it.

Pure and view functions are important concepts in Solidity, as they can help to improve the security and reliability of contracts. By using pure and view functions, contracts can be made more predictable and less susceptible to errors.

 

Here are some tips for using pure and view functions effectively in Solidity:

  • Use pure functions for calculations and data validation.
  • Use view functions to retrieve data from the contract.
  • Avoid modifying the state of the contract in view functions.
  • Be careful not to call functions that modify the state of the contract from within pure and view functions. This could lead to unexpected results.

By following these tips, you can use pure and view functions to improve the security and reliability of your Solidity contracts.

// SPDX-License-Identifier: MIT
pragma solidity >= 0.7.0 < 0.9.0;

// Getter functions use view or pure when declaring.

contract Ex3 {
    
    // The View function can read the state, but it cannot modify the state.
    function myBlock(uint a) public pure returns(uint) {
        return a; 
    }

    uint public b = 4;

    // Pure function prohibits reading or modifying state.
    function myBlock2() public view returns(uint) {
        uint c = b + 5;
        return c;
    }

    // no Modifier

    uint public x = 3;

    function maBlock3() public returns (uint) { // no getter
        x = 4;
        return x;
    }
 
}
728x90

'Blockchain > Solidity' 카테고리의 다른 글

Solidity : event / emit  (0) 2023.10.16
Solidity : function / Parameter / wei / ether / gas  (0) 2023.10.16
Solidity : Visibility  (0) 2023.10.15
Solidity : Operator / Shift operator  (1) 2023.10.15
Solidity : constant  (0) 2023.10.15
Comments