Fallback Function in Solidity

In this post, we will learn what is fallback function & how to use them in Solidity Language.

Notice: This is one of the multi-post series of Learn Solidity - Build Decentralized Application in Ethereum. This is an attempt to teach you all about Solidity - A Programming Language for Ethereum Based Smart Contracts. If you want to take this as a video course please signup using below button.

[pastacode lang=”javascript” manual=”pragma%20solidity%200.4.8%3B%20%0A%0A%2F*%0A*%20%40title%20Example%20for%20Solidity%20Course%0A*%20%40author%20Ethereum%20Community%0A*%20%40notice%20Example%20for%20the%20Solidity%20Course%0A*%2F%0A%0Acontract%20Test%20%7B%0A%20%20%20%20%2F%2F%20This%20function%20is%20called%20for%20all%20messages%20sent%20to%0A%20%20%20%20%2F%2F%20this%20contract%20(there%20is%20no%20other%20function).%0A%20%20%20%20%2F%2F%20Sending%20Ether%20to%20this%20contract%20will%20cause%20an%20exception%2C%0A%20%20%20%20%2F%2F%20because%20the%20fallback%20function%20does%20not%20have%20the%20%22payable%22%0A%20%20%20%20%2F%2F%20modifier.%0A%20%20%20%20function()%20%7B%20x%20%3D%201%3B%20%7D%0A%20%20%20%20uint%20x%3B%0A%7D%0A%0A%0A%2F%2F%20This%20contract%20keeps%20all%20Ether%20sent%20to%20it%20with%20no%20way%0A%2F%2F%20to%20get%20it%20back.%0Acontract%20Sink%20%7B%0A%20%20%20%20%2F%2Fbelow%20line%20will%20make%20the%20default%20function%20payable%20by%20adding%20a%20payable%20modifier%20like%20this%0A%20%20%20%20function()%20payable%20%7B%20%7D%0A%7D%0A%0A%0Acontract%20Caller%20%7B%0A%20%20%20%20function%20callTest(Test%20test)%20%7B%0A%20%20%20%20%20%20%20%20test.call(0xabcdef01)%3B%20%2F%2F%20function%20hash%20does%20not%20exist%0A%20%20%20%20%20%20%20%20%2F%2F%20results%20in%20test.x%20becoming%20%3D%3D%201.%0A%20%20%20%20%7D%0A%7D” message=”” highlight=”” provider=”manual”/]



[otw_shortcode_button href=”https://www.toshblocks.com/solidity/function-modifiers-solidity-how-they-works/” size=”large” icon_type=”general foundicon-left-arrow” icon_position=”left” shape=”radius” color_class=”otw-blue”]Previous: Function Modifiers in Solidity [/otw_shortcode_button][otw_shortcode_button href=”https://www.toshblocks.com/solidity/abstract-contracts-solidity/” size=”large” icon_type=”general foundicon-right-arrow” icon_position=”right” shape=”radius” color_class=”otw-green”]Next: Abstract Contract in Solidity[/otw_shortcode_button]