Skip to content

Instantly share code, notes, and snippets.

@zac-williamson
Created December 22, 2021 10:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zac-williamson/2d0423f54b11b699689b22f6afa98b62 to your computer and use it in GitHub Desktop.
Save zac-williamson/2d0423f54b11b699689b22f6afa98b62 to your computer and use it in GitHub Desktop.
Return by value vs return parameters
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract TestReturnPatterns {
struct Big
{
uint256[256] data;
}
function returnByValue() internal pure returns (Big memory big)
{
for (uint256 i = 0; i < 256; ++i)
{
big.data[i] = i;
}
}
function returnParameter(Big memory big) internal pure {
for (uint256 i = 0; i < 256; ++i)
{
big.data[i] = i;
}
}
function testReturnByValue() public pure returns (uint256)
{
Big memory big;
big.data[0] = 0;
big = returnByValue();
return big.data[255];
}
function testReturnParameter() public pure returns (uint256)
{
Big memory big;
big.data[0] = 0;
returnParameter(big);
return big.data[255];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment