0
0
Blockchain / Solidityprogramming~10 mins

Function overloading in Blockchain / Solidity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Function overloading
Call function with args
Check function signatures
Match args to signature?
NoError: No matching function
Yes
Execute matched function
Return result
When a function is called, the program checks all versions of that function to find one whose input matches the call, then runs that version.
Execution Sample
Blockchain / Solidity
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 has two 'add' functions: one adds two numbers, the other adds three.
Execution Table
StepFunction CalledArgumentsSignature MatchedActionOutput
1add(2, 3)add(uint,uint)Execute two-arg add5
2add(1, 2, 3)add(uint,uint,uint)Execute three-arg add6
3add(4)No matchError: No matching functionError
💡 Execution stops when a matching function signature is found or error if none matches.
Variable Tracker
VariableStartAfter Call 1After Call 2After Call 3
aN/A214
bN/A32N/A
cN/AN/A3N/A
resultN/A56N/A
Key Moments - 2 Insights
Why does calling add(4) cause an error?
Because no function 'add' is defined with a single argument, as shown in execution_table row 3.
How does the program decide which 'add' function to run?
It matches the number and types of arguments to the function signatures, as seen in execution_table rows 1 and 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output when add(2, 3) is called?
AError
B6
C5
DUndefined
💡 Hint
Check execution_table row 1 under Output column.
At which step does the function call fail due to no matching signature?
AStep 3
BStep 2
CStep 1
DNone
💡 Hint
Look at execution_table row 3 for the error case.
If we add a function add(uint a) returning a, what would happen at step 3?
AStill error
BIt would execute and return 4
CReturn 0
DCompile error
💡 Hint
Refer to how matching signatures work in execution_table and concept_flow.
Concept Snapshot
Function overloading lets you have multiple functions with the same name but different inputs.
When called, the program picks the function whose inputs match the call.
If no match, it causes an error.
Useful to handle similar actions with different data.
In blockchain smart contracts, this helps keep code clean and clear.
Full Transcript
Function overloading means having several functions with the same name but different input types or counts. When you call such a function, the program looks at the inputs you gave and finds the matching function version to run. If it finds none, it gives an error. For example, a contract might have two 'add' functions: one adding two numbers, another adding three. Calling add(2,3) runs the two-argument version and returns 5. Calling add(1,2,3) runs the three-argument version and returns 6. Calling add(4) causes an error because no single-argument version exists. This helps write neat code that handles similar tasks with different inputs.