Bird
Raised Fist0
Blockchain / Solidityprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is function overloading in blockchain smart contracts?
easy
A. Writing functions without any input parameters
B. Defining multiple functions with the same name but different inputs
C. Calling a function from another contract
D. Using the same function name for different contracts

Solution

  1. Step 1: Understand the term 'function overloading'

    Function overloading means having multiple functions with the same name but different input types or counts.
  2. Step 2: Apply this to blockchain smart contracts

    In smart contracts, this allows handling different input scenarios with one function name.
  3. Final Answer:

    Defining multiple functions with the same name but different inputs -> Option B
  4. Quick Check:

    Function overloading = same name, different inputs [OK]
Hint: Same name, different inputs means overloading [OK]
Common Mistakes:
  • Confusing overloading with calling functions
  • Thinking overloading means same inputs
  • Mixing overloading with inheritance
2. Which of the following is the correct syntax for function overloading in Solidity?
easy
A. function transfer(uint amount) public {} function transfer(address to, uint amount) public {}
B. function transfer(uint amount) public {} function transfer(uint amount) private {}
C. function transfer(uint amount) public {} function transfer(uint amount) public {}
D. function transfer() public {} function transfer() public {}

Solution

  1. Step 1: Check function names and parameters

    Both functions are named 'transfer' but have different parameter lists (one with uint, one with address and uint).
  2. Step 2: Confirm this matches overloading rules

    Overloading requires same name but different inputs, which function transfer(uint amount) public {} function transfer(address to, uint amount) public {} shows correctly.
  3. Final Answer:

    function transfer(uint amount) public {} function transfer(address to, uint amount) public {} -> Option A
  4. Quick Check:

    Different inputs, same name = correct syntax [OK]
Hint: Different parameters, same name is correct overloading [OK]
Common Mistakes:
  • Using same parameters for both functions
  • Changing only visibility, not parameters
  • Defining identical functions twice
3. Given the Solidity code:
contract Example {
  function calculate(uint x) public pure returns (uint) {
    return x * 2;
  }
  function calculate(uint x, uint y) public pure returns (uint) {
    return x + y;
  }
}

What is the output of calculate(3) and calculate(3, 4)?
medium
A. 6 and 12
B. 3 and 7
C. 6 and 7
D. Compilation error

Solution

  1. Step 1: Analyze the first function call calculate(3)

    This calls the function with one uint parameter, returning 3 * 2 = 6.
  2. Step 2: Analyze the second function call calculate(3, 4)

    This calls the overloaded function with two uint parameters, returning 3 + 4 = 7.
  3. Final Answer:

    6 and 7 -> Option C
  4. Quick Check:

    Single input doubles, two inputs add [OK]
Hint: Match input count to correct function [OK]
Common Mistakes:
  • Assuming both calls use the same function
  • Mixing multiplication and addition results
  • Expecting compilation error due to overloading
4. Identify the error in this Solidity contract using function overloading:
contract Test {
  function send(address to) public {}
  function send(address to) public {}
}
medium
A. Functions have the same name and parameters causing a duplicate definition error
B. Functions must have different visibility to overload
C. Function names must be unique in a contract
D. Functions must return a value to overload

Solution

  1. Step 1: Check function names and parameters

    Both functions are named 'send' and have the exact same parameter list (address to).
  2. Step 2: Understand overloading rules

    Overloading requires different parameters; identical signatures cause duplicate definition error.
  3. Final Answer:

    Functions have the same name and parameters causing a duplicate definition error -> Option A
  4. Quick Check:

    Same name and parameters = error [OK]
Hint: Overloaded functions must differ in parameters [OK]
Common Mistakes:
  • Thinking visibility difference allows overloading
  • Believing function return type affects overloading
  • Assuming same name always allowed
5. You want to create a smart contract function deposit that accepts either a single uint amount or a uint amount with a string note. How would you overload the deposit function correctly?
hard
A. function deposit(uint amount) public {} function deposit(string memory note, uint amount) public {}
B. function deposit(uint amount) public {} function deposit() public {}
C. function deposit(uint amount) public {} function deposit(uint amount) public {}
D. function deposit(uint amount) public {} function deposit(uint amount, string memory note) public {}

Solution

  1. Step 1: Define first function with single uint parameter

    The first deposit function takes only the amount as uint.
  2. Step 2: Define second function with uint and string parameters

    The second deposit function adds a string note parameter after the uint amount.
  3. Step 3: Confirm parameter order and types differ

    Both functions share the name but differ in parameter count and types, valid overloading.
  4. Final Answer:

    function deposit(uint amount) public {} function deposit(uint amount, string memory note) public {} -> Option D
  5. Quick Check:

    Same name, different parameters = correct overloading [OK]
Hint: Overload by changing parameter count or types [OK]
Common Mistakes:
  • Swapping parameter order causing confusion
  • Using identical parameter lists
  • Omitting parameter types
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard