delweng
9 months ago  Karma: 54
What is a function modifier in Solidity

Hi, I'm learning Solidity by now, and I'm hang on function modifier. I want to know what is it used for, what is _; means in the modifier's declaration?

solidity
learning
delweng
9 months ago  Karma: 54
What is a function modifier in Solidity

Hi, I'm learning Solidity by now, and I'm hang on function modifier. I want to know what is it used for, what is _; means in the modifier's declaration?

solidity
learning

5 ANSWERS
zi
9 months ago Karma: 27

You can think of function modifier as an extension of a function.
Modifier code will run first when assigned by a function.
_; means to return the flow of execution to the original function code.

Function modifier is mainly used to check a condition prior to executing the function. So you could re-use the same modifier in multiple functions if you are checking for the same condition. It reduces code redundancy.

You can think of function modifier as an extension of a function.
Modifier code will run first when assigned by a function.
_; means to return the flow of execution to the original function code.

Function modifier is mainly used to check a condition prior to executing the function. So you could re-use the same modifier in multiple functions if you are checking for the same condition. It reduces code redundancy.

crypto.yugioh
9 months ago Karma: 716

Typically, a Modifier is used in smart contracts to check if certain conditions are met before executing the rest of the body of code in a function. For example the modifier onlyOwner() is used to make sure that the caller of a function is the owner of the contract. The symbol _; is used to continue executing the rest of function body. For learning solidity in a funny and good way, I suggest that you have look at Cryptozombies.

Typically, a Modifier is used in smart contracts to check if certain conditions are met before executing the rest of the body of code in a function. For example the modifier onlyOwner() is used to make sure that the caller of a function is the owner of the contract. The symbol _; is used to continue executing the rest of function body. For learning solidity in a funny and good way, I suggest that you have look at Cryptozombies.

eduardonunesp
9 months ago Karma: 797

Function modifiers are a special function which can't be called directly but is used to add some modification or extra check to a function, let say you have a function which requires the sender send some Ether, you can use a modifier function require at least 1 wei, like:

modifier oneWei() {
  require(msg.value >= 1 wei);
  _;
}

What happens here is, first line it reads "require the message sender, to send on or more than one wei to continue", if that statement returns false the function will be reverted and canceled. The second line the _; reads "from here continue the body of the function which that modifier is attached to". So, for instance:

function showTime() public oneWei() {
  // Call Spiderman
}

The steps will be, the user will call that function, and the modifier will be called first to confirm the require statement like so, after that the weird _; is just resume the function showTime by calling the Spiderman. Isn't so hard, to understand, after the _; continue the normal flow of the function. And you can attach as many modifiers you need, and they will stack and executes in the sequence of left to right, like:

function showTime() public oneWei() onlyOwner() {
  // Call Spiderman
}

It will be, call function showTime, require oneWei and require the owner of the contract. (owner because is a private Spiderman show).

Function modifiers are a special function which can't be called directly but is used to add some modification or extra check to a function, let say you have a function which requires the sender send some Ether, you can use a modifier function require at least 1 wei, like:

modifier oneWei() {
  require(msg.value >= 1 wei);
  _;
}

What happens here is, first line it reads "require the message sender, to send on or more than one wei to continue", if that statement returns false the function will be reverted and canceled. The second line the _; reads "from here continue the body of the function which that modifier is attached to". So, for instance:

function showTime() public oneWei() {
  // Call Spiderman
}

The steps will be, the user will call that function, and the modifier will be called first to confirm the require statement like so, after that the weird _; is just resume the function showTime by calling the Spiderman. Isn't so hard, to understand, after the _; continue the normal flow of the function. And you can attach as many modifiers you need, and they will stack and executes in the sequence of left to right, like:

function showTime() public oneWei() onlyOwner() {
  // Call Spiderman
}

It will be, call function showTime, require oneWei and require the owner of the contract. (owner because is a private Spiderman show).

flyq951
9 months ago Karma: 247

accoding to solidity document
Function Modifiers
Modifiers can be used to easily change the behaviour of functions. For example, they can automatically check a condition prior to executing the function. Modifiers are inheritable properties of contracts and may be overridden by derived contracts.

```pragma solidity 0.4.11;

contract owned {
function owned() public { owner = msg.sender; }
address owner;

// This contract only defines a modifier but does not use
// it: it will be used in derived contracts.
// The function body is inserted where the special symbol
// _; in the definition of a modifier appears.
// This means that if the owner calls this function, the
// function is executed and otherwise, an exception is
// thrown.
modifier onlyOwner {
require(msg.sender == owner);
_;
}
}

contract mortal is owned {
// This contract inherits the onlyOwner modifier from
// owned and applies it to the close function, which
// causes that calls to close only have an effect if
// they are made by the stored owner.
function close() public onlyOwner {
selfdestruct(owner);
}
}

contract priced {
// Modifiers can receive arguments:
modifier costs(uint price) {
if (msg.value >= price) {
_;
}
}
}

contract Register is priced, owned {
mapping (address => bool) registeredAddresses;
uint price;

function Register(uint initialPrice) public { price = initialPrice; }

// It is important to also provide the
// payable keyword here, otherwise the function will
// automatically reject all Ether sent to it.
function register() public payable costs(price) {
registeredAddresses[msg.sender] = true;
}

function changePrice(uint _price) public onlyOwner {
price = _price;
}
}

contract Mutex {
bool locked;
modifier noReentrancy() {
require(!locked);
locked = true;
_;
locked = false;
}

/// This function is protected by a mutex, which means that
/// reentrant calls from within msg.sender.call cannot call f again.
/// The return 7 statement assigns 7 to the return value but still
/// executes the statement locked = false in the modifier.
function f() public noReentrancy returns (uint) {
require(msg.sender.call());
return 7;
}
}


Multiple modifiers are applied to a function by specifying them in a whitespace-separated list and are evaluated in the order presented.

accoding to solidity document
Function Modifiers
Modifiers can be used to easily change the behaviour of functions. For example, they can automatically check a condition prior to executing the function. Modifiers are inheritable properties of contracts and may be overridden by derived contracts.

```pragma solidity 0.4.11;

contract owned {
function owned() public { owner = msg.sender; }
address owner;

// This contract only defines a modifier but does not use
// it: it will be used in derived contracts.
// The function body is inserted where the special symbol
// _; in the definition of a modifier appears.
// This means that if the owner calls this function, the
// function is executed and otherwise, an exception is
// thrown.
modifier onlyOwner {
require(msg.sender == owner);
_;
}
}

contract mortal is owned {
// This contract inherits the onlyOwner modifier from
// owned and applies it to the close function, which
// causes that calls to close only have an effect if
// they are made by the stored owner.
function close() public onlyOwner {
selfdestruct(owner);
}
}

contract priced {
// Modifiers can receive arguments:
modifier costs(uint price) {
if (msg.value >= price) {
_;
}
}
}

contract Register is priced, owned {
mapping (address => bool) registeredAddresses;
uint price;

function Register(uint initialPrice) public { price = initialPrice; }

// It is important to also provide the
// payable keyword here, otherwise the function will
// automatically reject all Ether sent to it.
function register() public payable costs(price) {
registeredAddresses[msg.sender] = true;
}

function changePrice(uint _price) public onlyOwner {
price = _price;
}
}

contract Mutex {
bool locked;
modifier noReentrancy() {
require(!locked);
locked = true;
_;
locked = false;
}

/// This function is protected by a mutex, which means that
/// reentrant calls from within msg.sender.call cannot call f again.
/// The return 7 statement assigns 7 to the return value but still
/// executes the statement locked = false in the modifier.
function f() public noReentrancy returns (uint) {
require(msg.sender.call());
return 7;
}
}


Multiple modifiers are applied to a function by specifying them in a whitespace-separated list and are evaluated in the order presented.
crzndx
9 months ago Karma: 1182

Think of a modifier like a function, that is executed before or after the function, that the modifier is attached to.

The place of the underscore _; in your modifier declaration decides, if it is executed before or after.

modifier somethingBefore() {
    require(something);
    _; // resume with function testFunction()
}

modifier somethingAfter() {
    _;
    require(something);
}

function testFunction() somethingBefore somethingAfter {
    // executes somethingBefore here
    // -- your testFunction's payload --
    // executes somethingAfter here
}

Think of a modifier like a function, that is executed before or after the function, that the modifier is attached to.

The place of the underscore _; in your modifier declaration decides, if it is executed before or after.

modifier somethingBefore() {
    require(something);
    _; // resume with function testFunction()
}

modifier somethingAfter() {
    _;
    require(something);
}

function testFunction() somethingBefore somethingAfter {
    // executes somethingBefore here
    // -- your testFunction's payload --
    // executes somethingAfter here
}

Earn tokens by posting and answering questions about blockchain!
Karma to eth
YOUR ANSWER