Function overloading in Blockchain / Solidity - Time & Space Complexity
Function overloading lets us use the same function name with different inputs in blockchain code.
We want to see how this affects the time it takes to run the code as inputs change.
Analyze the time complexity of the following code snippet.
contract Example {
function add(uint a, uint b) public pure returns (uint) {
return a + b;
}
function add(uint a, uint b, uint c) public pure returns (uint) {
return a + b + c;
}
}
This contract shows two "add" functions with different numbers of inputs.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Simple addition operations inside each function.
- How many times: Each function runs its additions once per call; no loops or recursion.
Adding more numbers means more additions, but each function runs only once per call.
| Input Size (number of parameters) | Approx. Operations |
|---|---|
| 2 | 1 addition |
| 3 | 2 additions |
| 10 | 9 additions (if function existed) |
Pattern observation: Operations grow linearly with the number of inputs, but each call runs only one function version.
Time Complexity: O(1)
This means the time to run the function is constant regardless of the number of inputs, since each function version has a fixed number of parameters.
[X] Wrong: "Function overloading makes the program run slower because it tries all versions every time."
[OK] Correct: The blockchain chooses the right function based on inputs, so only one version runs each time.
Understanding how function overloading affects time helps you explain how your code scales and runs efficiently in blockchain projects.
"What if the overloaded functions included loops inside? How would that change the time complexity?"