✅03 - Return42
Goal: Return the number 42
from Yul.
Return42.t.sol
Returning an integer uses the same mechanism as returning a bool, but with a different value. We store the 256-bit representation of 42 in memory, then return 32 bytes. The number 42 (0x2A in hex) will be padded to 32 bytes (meaning 42 in the lowest bits and zeros elsewhere).
Return42.sol
Explanation: We use mstore(0, 42)
to place the value 42 at memory location 0. Then return(0, 32)
sends the 32-byte value back. The receiving side will interpret those 32 bytes as a uint256
equal to 42.
Run test:
Last updated