Created
December 22, 2021 10:16
-
-
Save zac-williamson/2d0423f54b11b699689b22f6afa98b62 to your computer and use it in GitHub Desktop.
Return by value vs return parameters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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