
- Solidity Tutorial
- Solidity - Home
- Solidity - Overview
- Solidity - Environment Setup
- Solidity - Basic Syntax
- Solidity - First Application
- Solidity - Comments
- Solidity - Types
- Solidity - Variables
- Solidity - Variable Scope
- Solidity - Operators
- Solidity - Loops
- Solidity - Decision Making
- Solidity - Strings
- Solidity - Arrays
- Solidity - Enums
- Solidity - Structs
- Solidity - Mappings
- Solidity - Conversions
- Solidity - Ether Units
- Solidity - Special Variables
- Solidity - Style Guide
- Solidity Functions
- Solidity - Functions
- Solidity - Function Modifiers
- Solidity - View Functions
- Solidity - Pure Functions
- Solidity - Fallback Function
- Function Overloading
- Mathematical Functions
- Cryptographic Functions
- Solidity Common Patterns
- Solidity - Withdrawal Pattern
- Solidity - Restricted Access
- Solidity Advanced
- Solidity - Contracts
- Solidity - Inheritance
- Solidity - Constructors
- Solidity - Abstract Contracts
- Solidity - Interfaces
- Solidity - Libraries
- Solidity - Assembly
- Solidity - Events
- Solidity - Error Handling
- Solidity Useful Resources
- Solidity - Quick Guide
- Solidity - Useful Resources
- Solidity - Discussion
Solidity - Function Overloading
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 list. You cannot overload function declarations that differ only by return type.
Following example shows the concept of a function overloading in Solidity.
Example
pragma solidity ^0.5.0; contract Test { function getSum(uint a, uint b) public pure returns(uint){ return a + b; } function getSum(uint a, uint b, uint c) public pure returns(uint){ return a + b + c; } function callSumWithTwoArguments() public pure returns(uint){ return getSum(1,2); } function callSumWithThreeArguments() public pure returns(uint){ return getSum(1,2,3); } }
Run the above program using steps provided in Solidity First Application chapter.
Click callSumWithTwoArguments button first and then callSumWithThreeArguments button to see the result.
Output
0: uint256: 3 0: uint256: 6
Advertisements