# solidity的函数修饰符

By [leishen](https://paragraph.com/@ldplxp) · 2022-05-17

---

修饰符是可以在一个函数调用之前或之后运行的代码。

有三种形式：基础修饰符，带参数，夹在中间的(sandwich)

修饰符可以用来：

*   限制访问
    
*   验证输入
    
*   防止重入黑客攻击
    

    contract FunctionModifier{
        bool public paused;
        uint public count;
    
        modifier whenNotPaused(){
            require(!paused, "the contract is paused");
            // _;的意思是返回主函数
            _;
        }
    
        modifier cap(uint _x){
            require(_x < 100, "X >= 100");
            _;
        }
    
        modifier sandwich(){
            count += 10;
            _;
            count *= 2;
        }
        // 函数修饰符可以用一大堆
        function incBy(uint _x) external whenNotPaused cap(_x) sandwich{
            count += _x;
        }
    }

---

*Originally published on [leishen](https://paragraph.com/@ldplxp/solidity)*
