我们可以使用 constant 和 returns 关键字和 a; 改变 solidity 中变量的状态吗?

Can we use constant and returns keyword and a;change the state of the variables in solidity?

警告说该函数被声明为视图并且那些行正在改变状态。我想要一个 return 语句,但也想在函数体中进行加法和减法(即,改变状态)。有可能吗? (此代码是 ERC20 令牌的一部分

function _transferToken(address _from, address _to, uint _value) constant public returns (string) 
 {
    // Prevent transfer to 0x0 address. Use burn() instead
    if(_to == 0x0)
    {
        return "Invalid address";
    }

    // Check if the sender has enough
    else if(balanceOf[_from] < _value)
    {
        return "insufficient tokens";
    }

    // Check for overflows
    else if(balanceOf[_to] + _value < balanceOf[_to])
    {
        return "Transaction failed";
    }

    else
    {
        // Subtract from the sender
        balanceOf[_from] = balanceOf[_from] - _value; ***warning***
        // Add the same to the recipient
        balanceOf[_to]=balanceOf[_to] + _value; *****warning*****
        return("Successful");

    }
 }

如果将函数声明为 view,则不得修改状态。这还没有强制执行,编译器只会发出警告。但是使用 view 函数无法实现您计划执行的操作。

从链下调用 view 函数不会花费您任何以太币,因为您不会更改状态或 运行 实际区块链上的任何计算。

此外,您不应该再对函数使用 constant 修饰符。它是 view 的别名,已弃用。
如官方文档所述,它将在 0.5.0 版本中被删除 here.