Advanced Functions

Function Overloading

Beginner Level

Function overloading occurs when multiple functions share the same name but have different parameter types or numbers. Solidity selects the correct function at compile time based on the arguments.

Example:


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

contract OverloadExample {
    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;
    }
}
  • add(2,3) calls the first function.
  • add(2,3,4) calls the second function.

Continue Learning

Explore more topics in Solidity or browse other tutorials.