In this track: Write and read Solidity
advanced · 4 MIN READ

Receive and fallback functions

Special entry points handle certain calls outside ordinary named-function dispatch.

LET’S PICTURE IT

A reception desk can have one route for simple deliveries and another for requests that do not match a named service.

SEE IT FOR YOURSELF
A valid counter call, step by stepStep 1 / 4
A valid counter call, step by stepFunction requestincrement()No argumentsBefore executionstored count0A local teaching example with a defined starting stateIncoming callCheck function dispatchCheck coded conditionsRules passConditions passContinue executionA failed check can revert insteadCalculation0 + 1Result: 1Storage writecount = 1Persists if call succeedsCalculating a local value and storing it are separate stepsPersistent statecount1Later readGetter reports 1No new incrementState, logs and browser display have different roles

This example begins with count zero and a request to call increment(). No real contract is deployed.

Animated local teaching model. No wallet, RPC or live blockchain data. Some labels and hashes are abbreviated for readability.

Receive handles a specific case

A payable receive function can handle an empty-calldata native-value call under Solidity dispatch rules. Its availability does not mean every conceivable ETH receipt path executes the same code.

Fallback catches unmatched dispatch

A fallback function handles calls that do not match an ordinary function, subject to the language’s receive/fallback selection rules. It needs payable capability to accept applicable native-value calls.

Proxies often use fallback machinery

A proxy can forward unmatched calldata to another implementation. That can change the effective function behavior while preserving a stable proxy address.

Keep safety separate

Receiving or forwarding code can fail and can involve external-call risks. The toy below accepts value but provides no withdrawal path, so it is a dispatch example rather than a recommended place to send assets.

Read the example

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

contract LearningExample {
    event Received(address sender, uint256 amount);

    receive() external payable {
        emit Received(msg.sender, msg.value);
    }

    fallback() external {
        revert("Unknown function");
    }
}

Educational dispatch example only: it intentionally has no withdrawal function.

QUICK CHECK · NO PRESSURE

Does a receive function handle every unmatched nonempty-calldata call instead of fallback?

Your progress stays in this browser.
Keep exploring the original referenceSolidity documentation ↗

Original explanations by EtherTools. Examples are educational and source-linked; this site is independent of the Ethereum Foundation. Our approach.