Skip to main content
This page is a placeholder. All examples on this page are currently AI-generated and are not correct. This documentation will be completed in the future with accurate, tested examples.

Overview

Opcode: 0xf3 Introduced: Frontier (EVM genesis) RETURN halts execution successfully and returns output data to the caller. All state changes are preserved, and the specified memory range is copied to the return buffer. This is the standard way to complete execution with a return value in the EVM.

Specification

Stack Input:
Stack Output: None Gas Cost: Memory expansion cost (dynamic) Operation:

Behavior

RETURN terminates execution with output:
  1. Pops offset from stack (top)
  2. Pops length from stack (second)
  3. Validates offset and length fit in u32
  4. Charges gas for memory expansion to offset+length
  5. Copies length bytes from memory[offset] to output buffer
  6. Sets execution state to stopped
  7. Returns control to caller with success status
State Effects:
  • All state changes preserved (storage, logs, balance transfers)
  • Output data available to caller
  • Remaining gas NOT refunded (consumed by transaction)
  • Execution marked as successful

Examples

Basic Return

Return Value

Compiled to:

Return String

Compiled (simplified):

Empty Return

Constructor Return

Constructor bytecode:

Gas Cost

Cost: Memory expansion cost (dynamic) Formula:
Examples: Return 32 bytes (1 word):
Return 256 bytes (8 words):
Return from existing memory (no expansion):

Edge Cases

Zero Length Return

Large Return Data

Out of Bounds

Offset Overflow

Stack Underflow

Out of Gas

Common Usage

Function Return Values

Compiled to:

Multiple Return Values

Compiled to:

Constructor Deployment

Constructor ends with RETURN containing runtime bytecode:

View Function

Compiled to:

Implementation

Testing

Test Coverage

Security

Return Data Size

Large return data consumes significant gas:
Gas cost grows quadratically with memory expansion:
  • 1 KB: ~100 gas
  • 10 KB: ~1,500 gas
  • 100 KB: ~150,000 gas

Memory Expansion Attack

Attacker cannot cause excessive memory expansion via RETURN:
  • Gas limit prevents unlimited expansion
  • Quadratic cost makes large expansion expensive
  • Out-of-gas reverts transaction

Return Data Validation

Caller must validate returned data:
Safe pattern:

State Finality

RETURN makes all state changes final:
Better: Validate before state changes.

Compiler Behavior

Function Returns

Solidity encodes return values using ABI encoding:
Compiled to:

Constructor Pattern

Every constructor ends with RETURN:
Bytecode structure:

View Functions

View functions use RETURN to provide read-only data:
Staticcall context + RETURN = gas-efficient reads.

References