![](/img/trans.png)
[英]In Solidity, what is the difference between using if() and require()?
[英]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。
如果您在外部调用view
或pure
函数,则无需支付 gas 费。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.