Receive Function

The receive function is a special function in Solidity that is called when a contract receives Ether without any data. It is used to receive Ether and is executed automatically when a contract receives Ether.
The receive function has the following characteristics:
  • It has no arguments or return values
  • It is marked as external
  • It is only executed if a transaction is sent to the contract with no data
  • It can receive Ether and perform additional operations
Here is an example of a receive function that simply emits an event when Ether is received:
contract MyContract {
event Received(address sender, uint amount);
receive() external payable {
emit Received(msg.sender, msg.value);
In this example, the receive function is external, meaning it can be called by anyone externally. The function is also payable, meaning it can receive Ether. When Ether is received, the function emits a Received event with the sender's address and the amount of Ether received as parameters.