Skip to main content
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
added 416 characters in body
Source Link
Manan Mehta
  • 360
  • 1
  • 2
  • 9

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
    }
}

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
    }
}

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
    }
}
Source Link
Manan Mehta
  • 360
  • 1
  • 2
  • 9

Examples to clarify Storage vs Memory assignments / return parameters

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
    }
}