1

I'm trying to call individual values from another function, without storing those values to the contract. Here is my complete code:

pragma solidity ^0.4.23;

contract Test {

    function setThreeValues() public view returns(int, uint, uint) {
        int x = 5;
        uint y = 7;
        uint z = 0;

        x += 5;
        y += 13;
        z = uint(x) + y;

        return (x, y, z);

    }



    function callThreeValues() public view returns(int, uint, uint) {
        return setThreeValues();
    }


    // function callX() public view returns(int) {
    //     return setThreeValues(int);
    // }


    // function callY() public view returns(uint) {
    //     return setThreeValues(uint);
    // }

    // function callZ() public view returns(uint) {
    //     return setThreeValues(uint);
    // }

}

when I run my function callThreeValues, I get all three values I'm expecting, which is 10, 20, 30.

However with my three further functions (callX, callY, callZ), I can't seem to get them to work....

with callX I'm trying to call the first value (10)

with callY I'm expecting to call the second value (20)

with callZ I'm expecting to call the third value (30)

I've obviously got the syntax wrong in my code, but I can't figure out how to get these functions working.

Sorry it's such a noob question!

Any help would be much appreciated :-)

1
  • Ask yourself what is the meaning of invoking setThreeValues(int)... Hint: you're not supposed to pass a type to a function (certainly when it doesn't expect any input arguments!!!). May 8, 2018 at 12:26

1 Answer 1

0

You can fix it as follows:

function callX() public view returns(int) {
    var (a, , ) = setThreeValues();
    return a;
}

function callY() public view returns(uint) {
    var (, b, ) = setThreeValues();
    return b;
}

function callZ() public view returns(uint) {
    var (, , c) = setThreeValues();
    return c;
}

But please note, if I'm not mistaken, that the var keyword has been deprecated at Solidity 0.4.21 (so you'll have to read the spec for an alternative).

BTW, you can change view to pure in all functions, since none of them reads state (global) variables.


And of course, you could always simply do:

int a;
uint b;
uint c;
(a, b, c) = setThreeValues();

And then return whichever value you want (a or b or c).

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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