1

Why do Solidity transact functions not return any value?

Here is an easy example. I tested it in remix and web3.


pragma solidity ^0.4.25;
contract SimpleStorage {

    uint public storedData;

    constructor(uint initVal) public {
        storedData = initVal;
    }

    function set(uint x) public returns (uint retVal) {
        storedData = x;
        return storedData;
    }

    function get() view public returns (uint retVal) {
        return storedData;
    }

}

I want to call the SET function of this contract and immediately get the recently assigned value returned without calling the GET.

Therefore, I return the value I just sent. However, this does not seem to work.

What I get returned is:

Returned Values

Take note that the value of decoded output is empty.

Is there a way to let the SET-Function output the value it just assigned?

  • 1
    They return, but you can only use it on-chain (i.e., when you call the function from this contract or from another contract). When you use it off-chain (i.e., when you call the function from a web3 client), you get the transaction receipt instead. In order to know what the return value was, you need to emit an event from the contract function, and that event will be available to you in the transaction receipt. – goodvibration Aug 9 at 13:37
  • 2
    Possible duplicate of What is the difference between a transaction and a call? – Ismael Aug 9 at 16:56
  • As mentioned before it's impossible to get the result without calling get or using events but why would you do that?what's the point? – Badr Bellaj Aug 11 at 12:03
2

The value is actually returned, but we just don't have a way to get it with web3 libraries yet. Think about it: how can your client get the return value if it has no clue when or if the transaction will be included in a block.

Luckily for you, there's a solution. It's called "Events", check it out: https://solidity.readthedocs.io/en/v0.5.10/structure-of-a-contract.html?highlight=events#events

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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