Learn Solidity: Function Modifiers & How They Works

In this post, we will understand what are function modifiers & 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%20Learn%20Solidity%3A%20Function%20Modifiers%20in%20Solidity%0A*%20%40author%20Toshendra%20Sharma%0A*%20%40notice%20Example%20for%20the%20Learn%20Solidity%0A*%2F%0A%0A%2F%2F%20Let’s%20talk%20about%20one%20of%20the%20most%20useful%20feature%20known%20as%20%22function%20modifiers%22%20in%20solidity%0A%2F%2F%20Modifiers%20can%20be%20used%20to%20easily%20change%20the%20behaviour%20of%20functions%2C%20%0A%2F%2F%20for%20example%20they%20can%20be%20used%20to%20to%20automatically%20check%20a%20condition%20prior%20to%20executing%20the%20function.%20%0A%2F%2F%20They%20are%20inheritable%20properties%20of%20contracts%20and%20may%20be%20overridden%20by%20derived%20contracts.%0A%0A%2F%2F%20one%20example%20use%20case%20of%20function%20modifiers%20would%20be%20if%20we%20want%20to%20call%20the%20killContract%20function%20%0A%2F%2F%20through%20only%20via%20owner%2Fcreator%20of%20the%20contract.%0A%0A%0A%0Acontract%20FunctionModifiers%20%7B%0A%0A%09address%20public%20creator%3B%0A%20%20%20%20%2F%2F%20Define%20consutruct%20here%0A%20%20%20%20function%20FunctionModifiers()%20%7B%0A%20%20%20%20%20%20%20%2F%2F%20Initialize%20state%20variables%20here%0A%20%20%20%20%20%20%20creator%20%3D%20msg.sender%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20%2F%2Fthis%20is%20how%20we%20define%20the%20modifiers%0A%20%20%20%20modifier%20onlyCreator()%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20if%20a%20condition%20is%20not%20met%20then%20throw%20an%20exception%0A%20%20%20%20%20%20%20%20if%20(msg.sender%20!%3D%20creator)%20throw%3B%0A%20%20%20%20%20%20%20%20%2F%2F%20or%20else%20just%20continue%20executing%20the%20function%0A%20%20%20%20%20%20%20%20_%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20%2F%2F%20this%20is%20how%20we%20add%20a%20modifier%20to%20the%20function%20%0A%20%20%20%20%2F%2F%20there%20can%20be%20zero%20of%20more%20number%20of%20modifiers%0A%20%20%20%20function%20kill()%20onlyCreator%20%7B%20%0A%20%20%20%20%09selfdestruct(creator)%3B%0A%20%20%20%20%7D%0A%0A%7D” message=”” highlight=”” provider=”manual”/]



[otw_shortcode_button href=”https://www.toshblocks.com/solidity/function-calls-return-types-solidity/” size=”large” icon_type=”general foundicon-left-arrow” icon_position=”left” shape=”radius” color_class=”otw-blue”]Previous: Function Calls & Return Types in Solidity [/otw_shortcode_button][otw_shortcode_button href=”https://www.toshblocks.com/solidity/fallback-function-solidity/” size=”large” icon_type=”general foundicon-right-arrow” icon_position=”right” shape=”radius” color_class=”otw-green”]Next: Fallback Function in Solidity[/otw_shortcode_button]