2

I have a function with 4 return values which are in different types. Within the same contract, I want another function get this return values. For example,

function myFunction1() returns (uint, string, address){
       ......
}
funtion myFunction2(){
    String s = myFunction1();
}

I want string s get the second value returned by myFunction1. How can I do that?

3

See the section on "destructuring assignments and returning multiple values" in the Solidity documentation.

As an example:

    function f() returns (uint, bool, uint) {
        return (7, true, 2);
    }

    function g() {
        // Declares and assigns the variables. Specifying the type explicitly is not possible.
        var (x, b, y) = f();
    }

However, it should be noted that you can't return dynamically sized values, as per this previous answer.

2

Here is the official example from the latest version of Solidity:

pragma solidity >0.4.23 <0.5.0;

contract C {
    uint[] data;

    function f() public pure returns (uint, bool, uint) {
        return (7, true, 2);
    }

    function g() public {
        // Variables declared with type and assigned from the returned tuple.
        (uint x, bool b, uint y) = f();
        // Common trick to swap values -- does not work for non-value storage types.
        (x, y) = (y, x);
        // Components can be left out (also for variable declarations).
        (data.length,,) = f(); // Sets the length to 7
    }
}

Note that you can now use dynamically sized values.

Furthermore, the var keyword has been deprecated.

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.