CH16 Overloading

  1. Function overloading

    1. Different Parameters: Functions with the same name can exist in a contract, as long as they have different parameter types or different numbers of parameters.
    2. Modifiers: Unlike functions, Solidity does not allow overloading of modifiers.
  2. Argument Matching:

    When calling an overloaded function, Solidity matches the input arguments with the function parameters. If multiple functions match the input arguments, a compilation error will occur.

    Example:

    function f(uint8 _in) public pure returns (uint8 out) {    
        out = _in;
    }
    
    function f(uint256 _in) public pure returns (uint256 out) {    
        out = _in;
    }
    //  you can call the function f() with either a uint8 or uint256 argument
    

    If you specifically want to call the version of f() that accepts a uint8, you need to provide the exact type:

    f(100); // Solidity will use the version of f() that accepts uint256 by default.
    
    f(uint8(100)); // This explicitly calls the f() that accepts uint8.
    
    

    Errors in Overloading: If the input parameters are ambiguous and match more than one function, Solidity will throw a compilation error. This ensures that the code behaves predictably.