繁体   English   中英

solidity 中的纯修饰符和视图修饰符有什么区别?

[英]What is the difference between pure and view modifiers in solidity?

该代码导致相同的 output。

pragma solidity ^0.5.0;
contract mycontract
{

   function add(uint c, uint d) public pure returns(uint)
  { uint e=c+d;


   return e;

  } 
   function add(uint j, uint k) public view returns(uint)
  { uint f=j+k;


   return f;

  } 

}

pure不查看也不修改 state 即它只能使用提供给它的东西来运行。 view不能修改state,但可以查一下

view表明 function 不会以任何方式更改存储 state。 但它允许你“查看”它

pure更加严格,表示它甚至不会读取存储 state。

一个纯 function 是一个 function,它给定相同的输入,总是返回相同的 output。 但是合约的 state 随着用户与之交互而不断变化。 So if you pass a state variable as an argument to the function, since the state is changing, that function will not be pure. 这就是为什么纯函数无法访问 state。

如果您在外部调用viewpure函数,则无需支付 gas 费。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM