0
0
Blockchain / Solidityprogramming~10 mins

Receive and fallback functions in Blockchain / Solidity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Receive and fallback functions
Contract receives Ether
Is msg.data empty?
NoCall fallback()
Yes
Call receive()
Execute function body
End
When a contract receives Ether, it checks if data is sent. If no data, receive() runs; if data or no receive(), fallback() runs.
Execution Sample
Blockchain / Solidity
contract Example {
  receive() external payable {
    // handle plain Ether
  }
  fallback() external payable {
    // handle calls with data or no receive
  }
}
This contract has receive() for plain Ether and fallback() for calls with data or when receive() is missing.
Execution Table
Stepmsg.dataFunction CalledActionOutput
1emptyreceive()Accept Ether with no dataEther received
2non-emptyfallback()Handle call with dataFallback logic runs
3emptyfallback()No receive() defined, fallback handles EtherEther received via fallback
4non-emptyfallback()Fallback called for unknown functionFallback logic runs
5---Execution ends
💡 Execution stops after receive() or fallback() handles the call
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
msg.datavariesemptynon-emptyemptynon-emptyvaries
Function Callednonereceive()fallback()fallback()fallback()none
Key Moments - 2 Insights
Why does fallback() run when msg.data is empty in step 3?
Because receive() is not defined in that case, fallback() handles the Ether receipt as shown in execution_table row 3.
What happens if msg.data is non-empty but receive() exists?
Fallback() runs because receive() only handles calls with empty msg.data, as shown in execution_table row 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, which function is called when msg.data is empty and receive() exists?
Areceive()
Bfallback()
Cconstructor()
Dno function
💡 Hint
Check execution_table row 1 where msg.data is empty and receive() is called
At which step does fallback() handle Ether because receive() is missing?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at execution_table row 3 where fallback() runs with empty msg.data
If msg.data is non-empty, which function always runs according to the table?
Areceive()
Bconstructor()
Cfallback()
Dno function
💡 Hint
See execution_table rows 2 and 4 where msg.data is non-empty and fallback() runs
Concept Snapshot
receive() runs when contract gets Ether with empty msg.data
fallback() runs when msg.data is non-empty or receive() is missing
Both can be payable to accept Ether
Use receive() for plain Ether transfers
Use fallback() for calls with data or unknown functions
Full Transcript
When a contract receives Ether, it checks if the data sent (msg.data) is empty. If empty and receive() exists, receive() runs to accept Ether. If msg.data is not empty or receive() is missing, fallback() runs. This lets contracts handle plain Ether transfers and calls with data differently. The execution table shows steps where receive() or fallback() is called based on msg.data. Variables msg.data and Function Called change accordingly. Key moments clarify why fallback() runs sometimes even with empty msg.data. The visual quiz tests understanding of which function runs in different cases.