Solidity

Solidity Function Modifiers

Solidity Function Modifiers Main Tips

  • In Solidity function modifiers are used to conveniently modify function behavior.
  • Modifiers are also inheritable contract properties, that can be overriden by contracts deriving from it.

Solidity Function Modifiers

In Solidity function modifiers are used to conveniently modify function behavior. For instance, you can make it so a condition is checked before a function is executed.

Modifiers are also inheritable contract properties, that can be overriden by contracts deriving from it.

Example

pragma solidity ^0.4.11;

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

    // The contract here will only define a modifier but will not 
    // use it - derived contracts are going to utilize it.
    // The body of the function is inserted where the special 
    // symbol "_;" in the modifier's definition appears.
    // That means that when the owner will call this function, 
    // the function will get executed and otherwise, an exception 
    // will be thrown.
    modifier ownerOnly {
        require(msg.sender == owner);
        _;
    }
}

contract mortal is isOwned {
    // The contract "mortal" will inherit the modifier "ownerOnly" 
    // from the contract "isOwned" and apply it to the function "close"
    // that will cause calls to "close" only affect if
    // the stored owner makes them.
    function close() ownerOnly {
        selfdestruct(owner);
    }
}

contract priced {
    // Modifiers may also receive arguments of their own:
    modifier itCosts(uint price) {
        if (msg.value >= price) {
            _;
        }
    }
}

contract Registration is priced, isOwned {
    mapping (address => bool) addressesRegistered;
    uint price;

    function Registration(uint initialPrice) { price = initialPrice; }

    // The keyword "payable" is crucial here, else the function 
    // will automatically reject all Ether that gets 
    // sent to it.
    function Registration() payable itCosts(price) {
        addressesRegistered[msg.sender] = true;
    }

    function priceChange(uint _price) ownerOnly {
        price = _price;
    }
}

contract Mutex {
    bool isLocked;
    modifier noReentrance() {
        require(!isLocked);
        isLocked = true;
        _;
        isLocked = false;
    }

    /// The function here is protected by mutex, meaning that
    /// reentrant will call from inside msg.sender.call, being unable to call func again.
    /// The statement return 7 will assign 7 to the value being returned but still
    /// execute the statement isLocked = false inside the modifier.
    function func() noReentrance returns (uint) {
        require(msg.sender.call());
        return 7;
    }
}

 

Try on Remix Try live on Hosting

You can apply multiple modifiers to a single function by writing them in a list separated by whitespaces. The modifiers are then evaluated in the presented order of the list.

Explicitly returning from a function body or modifier only leaves the current function body or modifier. Return variables are assigned and control flow would continue after the “_” in the modifier preceding it.

Arbitrary expressions are allowed only for modifier arguments and inside this context, every symbol visible from the function is also visible inside the modifier. Symbols that are introduced in the modifier, however, are not visible inside the function (since there is a possibility they may change by overriding)

Warning: Earlier, in a previous version of Solidity, return statements inside functions that have modifiers used to behave differently.

Read previous post:
Solidity Visibility and Getters

Solidity Visibility and Getters Main Tips Solidity provides two types of function calls: internal ones and external ones, which create an...

Close