1

How do I get a return value in solidity?

I'm using some test code in remix, but I don't understand where the output is displayed.

my code is as follows:

pragma solidity ^0.4.17;

contract Test {
    function getValue(string something) public returns (uint) {
        return 123;
    }
}

when I call the getValue method with a string "testing123" (quote marks required), I expected the function to return 123.

the properties returned on Remix are:

decoded input:

-

decoded output

{ "string something": "testing123" }

I have read the documentation but I may not be understanding some part of it. Any help is appreciated.

3

I did copy paste your code and it works fine (although there are some warnings!). Once you deployed the contract (with the deploy button) you can call your function with the getValue button.

enter image description here

You should see the result in the bottom gray box. It shouws the whole transaction and also the decoded output.

enter image description here

UPDATE

According to the yellow paper:

enter image description here

Essentially the output is used only when the message call is performed by the execution of a contract code. Otherwise, i.e. when the message call is due to a transaction (that triggers the execution) the output should be ignored, as reported.

7
  • that works for me too in the javascript vm environment, but i am trying to do this on the injected web3 environment with Metamask on ropsten. do you know why that wouldn't be working?
    – X0r0N
    May 30 '18 at 20:50
  • With a private local network (i.e. with web3) I have exactly your output "-".
    – Briomkez
    May 30 '18 at 21:11
  • no problem. is this some kind of bug on remix? i think the example contract looks too simple to have any bug.
    – X0r0N
    May 30 '18 at 21:14
  • Please take a look to my update.
    – Briomkez
    May 30 '18 at 21:16
  • thanks. i am fluent in english, but i am struggling to understand the wording from the yellow paper. solidity is not my expertise, are you able to explain why the example i created does not work?
    – X0r0N
    May 30 '18 at 21:23
2

Did you try to add the

view

modifier like so ? (also it's better if you do something of your string ;) :

pragma solidity ^0.4.17;

contract Test {

    function getValue(string something) public view returns (string, uint) {
        return (something, 123);
    }
}

works like a charm on remix.

1

It might be because web3 sends transactions to methods that do not have the view or the constant modifier, instead of making a call.

1

Excellent question. This was the same problem that had me stumped too, until I saw @Briomkez's answer with reference to the yellow paper as well as the other answers .

In case someone doesn't understand his excellent but slightly technical answer or needs to see it in code action, please see below:

Please copy-paste the below sample (modified from the usual Remix default storage sample for this purpose) into Remix. Deploy it using "Injected Web3" option in Remix which will use Metamask for deployment on any testnet like Ropsten/Rinkeby.

  • First set a value by calling the Store() method with a value (e.g. 2021). Note that this will not return any value in the decodedoutput field.
  • Next call the retrieve2() method and examine the output in the remix log window. The decodedoutput field will be empty.
  • Next call the retrieve() method and examine the decodedoutput field. It will show the value properly.

Code Sample


// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.7.0 <0.9.0;

contract Storage {

    uint number; //state variable

    function store(uint num) public returns (uint) {
        number = num;
        //even this will not return any value in the decoded output field
        return num;         }

    //the retrieve() view function will return a value in the 
   // "decodedoutput" field of the transaction

    function retrieve() public view returns (uint){ 
        return number;
    }

    // this method (not being a view method) will not return a value
    function retrieve2() public returns (uint){ 
        return number;
    }
}

Explanation: The Yellowpaper states that the decodedoutput field will be ignored in the case of transactions, but will be filled out in the case of view functions etc.

Hope this helps!

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.