Which is the best, view or pure instead of constant?

library SafeMath {
  function mul(uint256 a, uint256 b) internal constant returns (uint256) {
    uint256 c = a * b;
    assert(a == 0 || c / a == b);
    return c;

  function div(uint256 a, uint256 b) internal constant returns (uint256) {
    assert(b > 0); // Solidity automatically throws when dividing by 0
    uint256 c = a / b;
    assert(a == b * c + a % b); // There is no case in which this doesn't hold
    return c;

  function sub(uint256 a, uint256 b) internal constant returns (uint256) {
    assert(b <= a);
    return a - b;

  function add(uint256 a, uint256 b) internal constant returns (uint256) {
    uint256 c = a + b;
    assert(c >= a);
    return c;


  • Question doesn't show research effort. Please make sure you visit the available documentations before putting the question.
    – Aniket
    Nov 22, 2018 at 6:10
  • Disagree with the Downvote. This question could also have been a one-liner, and it would still have been of value. It gives an example, which is some effort as such. And the answers show that this one-line question gives many insights. Jan 6, 2022 at 19:06

3 Answers 3


View can be used to with a function that does not modify the state but reads state variables.

Pure should be used with functions that neither modify state nor read ( access) state variables. They generally perform operations based on input params.

An example illustrating the same is here:

pragma solidity ^0.4.24;

contract ViewVsPure {
  uint public age = 18;

  function addToAge(uint _no) public view returns (uint){
    return age + _no;

  function add(uint _a, uint _b) public pure returns (uint) {
    return _a + _b;

They're not the same.

view functions have read-only access permission on state variables.

pure functions do not.

Those functions that you mention can all be declared pure.

Side note: this library is provided in OpenZeppelin GitHub as open source, which means that it has been viewed, tested and verified thoroughly. No reason to implement it yourself.

  • yes but whats the more secure or convenient
    – upracha
    Nov 21, 2018 at 8:32
  • @upracha: You should choose the most strict option (and the compiler will warn you if you don't anyway). Nov 21, 2018 at 8:33

I think you cant find what you are looking for in the solidity official documentation here: https://solidity.readthedocs.io/en/v0.4.25/contracts.html?highlight=view%20pure#view-functions

Hope it helps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.