Solidity - Fall Back Function Last Updated : 06 Mar, 2023 Comments Improve Suggest changes 11 Likes Like Report The solidity fallback function is executed if none of the other functions match the function identifier or no data was provided with the function call. Only one unnamed function can be assigned to a contract and it is executed whenever the contract receives plain Ether without any data. To receive Ether and add it to the total balance of the contract, the fallback function must be marked payable. If no such function exists, the contract cannot receive Ether through regular transactions and will throw an exception. Properties of a fallback function: Declare with fallback() and have no arguments.If it is not marked payable, the contract will throw an exception if it receives plain ether without data.Can not return anything.Can be defined once per contract.It is also executed if the caller meant to call a function that is not available or receive() does not exist or msg.data is not empty.It is mandatory to mark it external.It is limited to 2300 gas when called by another function by using transfer() or send() method . It is so for as to make this function call as cheap as possible.Example: In the below example, the Contract is created to demonstrate different conditions for different fallback function. Solidity // SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.8.0 <0.9.0; /// @title A contract for demonstrate fallback function /// @author Jitendra Kumar /// @notice For now, this contract just show how to fallback function receive all the ether contract GeeksForGeeks { string public calledFallbackFun; // This fallback function // will keep all the Ether fallback() external payable{ calledFallbackFun="Fallback function is executed!"; } function getBalance() public view returns (uint) { return address(this).balance; } } // Creating the sender contract contract Sender { function transferEther() public payable { //paste the deployed contract address of GeeksForGeeks smart contract here (bool sent, ) = payable(0xD4Fc541236927E2EAf8F27606bD7309C1Fc2cbee).call{value: 2 ether}("Transaction Completed!"); require(sent, "Transaction Failed!"); } function getBalance() public view returns (uint) { return address(this).balance; } } Output: OutputExplanation:First deploy the GeeksForGeeks smart contract and paste the deployed smart contract address into the transferEther function of Sender smart contract.In the Sender smart contract we have also assigned the transfer value of Ether.After deploying the Sender smart contract, deposit more than or equal to the assigned Ether value in it at the time of calling the transferEther function. transferEther function has the call method to transfer the assigned Ether value from Sender smart contract to GeeksForGeeks smart contract and fallback function receives these Ethers.Call the calledFallbackFun function of GeeksForGeeks smart contract. It will return a string value "Fallback function is executed!" and also you can check the smart contract balance by using the getBalance function. Create Quiz Comment C ciberexplosion Follow 11 Improve C ciberexplosion Follow 11 Improve Article Tags : Solidity Blockchain Solidity-Functions Explore Solidity BasicsIntroduction to Solidity5 min readSetting Up Smart Contract Development Environment5 min readSolidity - Basic Syntax5 min read"Hello World" Smart Contract in Remix-IDE4 min readSolidity - Comments4 min readSolidity - Types4 min readVariable and OperatorsSolidity - Variables4 min readSolidity - Variable Scope3 min readSolidity - Operators7 min readControl Flow in SoliditySolidity - While, Do-While, and For Loop3 min readSolidity - Decision Making Statements4 min readSolidity - Break and Continue Statements2 min readReference & Mapping Types in SoliditySolidity - Strings3 min readSolidity - Arrays6 min readSolidity - Enums and Structs3 min readSolidity - Mappings4 min readSolidity - Conversions6 min readSolidity - Ether Units7 min readSolidity - Special Variables3 min readSolidity - Style Guide13 min readSolidity FunctionsSolidity - Functions4 min readSolidity - Function Modifiers8 min readSolidity - View and Pure Functions2 min readSolidity - Fall Back Function3 min readSolidity Function Overloading1 min readMathematical Operations in Solidity6 min readSolidity AdvancedSolidity - Basics of Contracts4 min readSolidity - Inheritance6 min readSolidity - Constructors4 min readSolidity - Abstract Contract3 min readSolidity - Basics of Interface2 min readSolidity - Libraries4 min readSolidity - Assembly4 min readWhat are Events in Solidity?2 min readSolidity - Error Handling6 min readTop 50 Solidity Interview Questions and Answers15+ min read Like