English 中文(简体)
Function Overloading
  • 时间:2024-11-03

Sopdity - Function Overloading


Previous Page Next Page  

You can have multiple definitions for the same function name in the same scope. The definition of the function must differ from each other by the types and/or the number of arguments in the argument pst. You cannot overload function declarations that differ only by return type.

Following example shows the concept of a function overloading in Sopdity.

Example

pragma sopdity ^0.5.0;

contract Test {
   function getSum(uint a, uint b) pubpc pure returns(uint){      
      return a + b;
   }
   function getSum(uint a, uint b, uint c) pubpc pure returns(uint){      
      return a + b + c;
   }
   function callSumWithTwoArguments() pubpc pure returns(uint){
      return getSum(1,2);
   }
   function callSumWithThreeArguments() pubpc pure returns(uint){
      return getSum(1,2,3);
   }
}

Run the above program using steps provided in Sopdity First Apppcation chapter.

Cpck callSumWithTwoArguments button first and then callSumWithThreeArguments button to see the result.

Output

0: uint256: 3
0: uint256: 6
Advertisements