ReasonJun

Solidity : Overloading 본문

Blockchain/Solidity

Solidity : Overloading

ReasonJun 2023. 10. 16. 23:55
728x90

Function overloading in Solidity allows you to define multiple functions with the same name but different argument types and/or numbers. This can be useful for creating more flexible and reusable code.

 

For example, the following contract defines an overloaded add() function:

contract Math {
  function add(uint256 a, uint256 b) public pure returns (uint256) {
    return a + b;
  }

  function add(uint256 a, uint256 b, uint256 c) public pure returns (uint256) {
    return a + b + c;
  }
}

This means that you can call the add() function with two or three arguments, depending on your needs. For example, the following code calls the add() function with two arguments:

Math math = new Math();
uint256 result = math.add(1, 2);

This will return the value 3.

The following code calls the add() function with three arguments:

Math math = new Math();
uint256 result = math.add(1, 2, 3);

This will return the value 6.

When you call an overloaded function, Solidity will choose the function with the best match for the argument types and numbers that you provided. If there is no exact match, Solidity will throw an error.

 

Here are some of the benefits of using function overloading in Solidity:

  • Increased flexibility: Function overloading allows you to create more flexible code that can be used in a variety of situations.
  • Reduced code duplication: Function overloading can help to reduce code duplication by allowing you to reuse code for different purposes.
  • Improved readability: Function overloading can make your code more readable by making it easier to understand what each function does.

Overall, function overloading is a powerful feature of Solidity that can be used to improve the quality, flexibility, and readability of your code.

 

Here are some tips for using function overloading effectively:

  • Use descriptive function names: Choose descriptive function names that make it clear what each function does. This will help to prevent confusion when you are calling overloaded functions.
  • Use visibility modifiers: Use visibility modifiers to restrict access to overloaded functions. This can help to improve the security of your code.
  • Document your code: Document your code to explain how overloaded functions are used. This will help other developers to understand and use your code correctly.

 

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;

contract Ex1{
    
    function get(uint _x) public pure returns (uint) {
        return _x;
    }

      function get(bool _x) public pure returns (bool) {
        return _x;
    }
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;

contract Calculator {
    function mul(uint _num1, uint _num2) public pure returns(uint) {
        return _num1 * _num2;
    }
     function mul(uint _num1, uint _num2, uint _num3) public pure returns(uint) {
        return _num1 * _num2 * _num3;
    }
   
}

contract Over {
   
    Calculator internal calculator = new Calculator();

     function getNumgers() public view returns(uint, uint) {
        return (calculator.mul(3, 9), calculator.mul(2,4,6));
    }
   
}

 

728x90

'Blockchain > Solidity' 카테고리의 다른 글

Solidity : abstract  (0) 2023.10.17
Solidity : Overriding (virtual / override / super)  (0) 2023.10.17
Solidity: Encapsulation  (0) 2023.10.16
Solidity : Inheritance (is, override / Multiple)  (0) 2023.10.16
Solidity : Immutable / Constant  (0) 2023.10.16
Comments