0

This question already has an answer here:

See the below contract function of which return the string (Tested in Remix.org):

pragma solidity >=0.4.22 <0.6.0;
contract HelloWorld {
    function get() pure public returns (string memory){
        return "Hello Solidity";
    }
}

While in the following contract, I can't get the returned string.

pragma solidity >=0.4.22 <0.6.0;
contract HelloWorld {
    uint num;
    function get(uint val) public returns (string memory){
        num = val;
        return "Hello Solidity";
    }
}

Why? And how can I get returned string from not pure function ?

marked as duplicate by eth Mar 25 at 19:53

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2

Web3 API does not allow obtaining return values of functions executed on-chain, i.e. in mined transactions. Though, in case other contract will execute your functions, it will get return value.

This is because values returned by transactions are not stored in blocks, so in order to obtain such value one will need to replay the transaction. You may replay the transaction via contract.methods.method.call(...).

  • I'm not using Web3 API, and I'm using go-ethereum source code to execute the contract. The return type of pure one is "string" but that of not pure one is "*types.Transaction". I think I need to replay the returned *types.Transaction. – Shuai Xu Mar 25 at 20:18

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