0
0
Blockchain / Solidityprogramming~20 mins

Function overloading in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Function Overloading Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of overloaded function calls in Solidity

Consider the following Solidity contract with overloaded functions named calculate. What will be the output when calling calculate(5) and calculate(5, 10)?

Blockchain / Solidity
pragma solidity ^0.8.0;

contract OverloadExample {
    function calculate(uint x) public pure returns (uint) {
        return x * 2;
    }

    function calculate(uint x, uint y) public pure returns (uint) {
        return x + y;
    }
}
Acalculate(5) returns 5; calculate(5, 10) returns 50
Bcalculate(5) returns 10; calculate(5, 10) returns 50
Ccalculate(5) returns 10; calculate(5, 10) returns 15
Dcalculate(5) returns 5; calculate(5, 10) returns 15
Attempts:
2 left
💡 Hint

Look at how each function processes its parameters: one doubles a single number, the other adds two numbers.

Predict Output
intermediate
2:00remaining
Function overloading with different parameter types

Given this Solidity contract, what is the output of calling getValue(3) and getValue(true)?

Blockchain / Solidity
pragma solidity ^0.8.0;

contract TypeOverload {
    function getValue(uint x) public pure returns (string memory) {
        return "Number";
    }

    function getValue(bool b) public pure returns (string memory) {
        return "Boolean";
    }
}
ABoth calls return "Boolean"
BgetValue(3) returns "Number"; getValue(true) returns "Boolean"
CBoth calls return "Number"
DgetValue(3) returns "Boolean"; getValue(true) returns "Number"
Attempts:
2 left
💡 Hint

Functions are chosen based on the parameter type passed.

🔧 Debug
advanced
2:00remaining
Identify the error in overloaded functions

Examine this Solidity contract with overloaded functions. What error will the compiler produce?

Blockchain / Solidity
pragma solidity ^0.8.0;

contract ErrorExample {
    function foo(uint x) public pure returns (uint) {
        return x + 1;
    }

    function foo(uint y) public pure returns (uint) {
        return y + 2;
    }
}
ACompiler warning: Function is deprecated
BRuntime error: Ambiguous function call
CNo error, compiles and runs fine
DCompiler error: Function with same name and parameter types already defined
Attempts:
2 left
💡 Hint

Check if the parameter lists differ between the two functions.

📝 Syntax
advanced
2:00remaining
Correct syntax for overloaded functions in Solidity

Which option shows the correct syntax for two overloaded functions named transfer in Solidity?

A
function transfer(address to, uint amount) public {}
function transfer(address to, uint amount, bool flag) public {}
B
function transfer(address to, uint amount) public {}
function transfer(uint amount, address to) public {}
C
function transfer(address to, uint amount) public {}
function transfer(address to) public {}
D
function transfer(address to, uint amount) public {}
function transfer(address to, uint amount) public {}
Attempts:
2 left
💡 Hint

Overloaded functions must differ in parameter types or number of parameters.

🚀 Application
expert
3:00remaining
Designing overloaded functions for token transfers

You want to design a Solidity contract with overloaded sendTokens functions:

  • One function sends tokens to a single address.
  • Another sends tokens to multiple addresses with amounts.

Which option correctly declares these overloaded functions?

A
function sendTokens(address to, uint amount) public {}
function sendTokens(address[] memory recipients, uint[] memory amounts) public {}
B
function sendTokens(address to, uint amount) public {}
function sendTokens(address[] recipients, uint[] amounts) public {}
C
function sendTokens(address to, uint amount) public {}
function sendTokens(address[] memory recipients, uint amount) public {}
D
function sendTokens(address to, uint amount) public {}
function sendTokens(address to, uint amount, bool batch) public {}
Attempts:
2 left
💡 Hint

Remember to use memory keyword for array parameters in Solidity.