1

Is there a mistake in funcB? I do not understand why/when to mention the variable name of the return parameter in the function signature.

contract Foo {

    // State variables go to Storage
    uint[] public foo;  

    function funcA(uint[] x) public returns (uint) { // x is in memory
        foo = x  // x is copied to storage now

        uint a = 100  // a is in storage
        return a
    }

    function funcB(uint[] x) public returns (uint a) { // x is in memory
        a = 100  // a is in memory? because `returns (uint a)`
        return a
    }
}

Also, I suppose funcA is incorrect but funcB is okay in the following example?

contract Foo {

    uint[] public foo;  

    function funcA() public returns (uint[]) { 
        uint a[];
        // push stuff to a
        return a
    }

    function funcB() public returns (uint a[]) {
        // push stuff to a
        return a
    }
}
1

Mentioning the return variable name is useful in case you want to return multiple results from a function.

for example:

function funcA() public returns (uint a,unit b,uint c){
    a = 1;
    b = 2;
    c = 3; 
}

this will output 3 return values.

0

in this case a is in memory. You can handle your return in TWO ways

function funcB(uint[] x) public returns (uint a) { // x is in memory
    uint z = 100;
    return z; // or return 100 directly 
}

or

function funcB(uint[] x) public returns (uint a) { // x is in memory
    a = 100;  // you have already assigned a to be 100
    return;
}

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.