[Ethereum] What’s the point of returning a value in a non-constant function

erc-20solidity

As we know you can't get the return value of a non-constant function
Still ERC20 does this a lot, for example:

 function approve(address _spender, uint256 _value) returns (bool success)

Hypothesis: return values are only there for contract-to-contract calls, and since you can't return anything from a transaction it is useless if not called by contracts.

Is this true? Are contract to contract calls with return values possible? And should you then always have a (Event, return) pair in this cases?

Best Answer

Yes, contract to contract return values are possible. Let's say you have this contract:

contract Tool {
    function numberIsEven() returns (bool);
}

We now call it from another contract:

import "Tool.sol";

contract Caller {
    Tool public tool;
    uint public saved;

    function Caller(address _tool) {
        tool = Tool(_tool);
        saved = 0;
    }

    function changeNumber() {
        if (tool.numberIsEven()) {
            saved = 2;
        } else {
            saved = 1;
        }
    }
}

Within your tests you can check what saved value is kept in the Caller contract.

I have created a small repo to demonstrate it. Look at the contracts and the tests, which contain comments.

Related Topic