0
0
Blockchain / Solidityprogramming~30 mins

Reference types behavior in Blockchain / Solidity - Mini Project: Build & Apply

Choose your learning style9 modes available
Understanding Reference Types Behavior in Blockchain Smart Contracts
📖 Scenario: Imagine you are building a simple blockchain smart contract to manage a list of users and their balances. You want to understand how reference types like arrays behave when you assign them to new variables or modify them inside functions.
🎯 Goal: You will create a smart contract that stores an array of balances. You will then create a function to demonstrate how modifying a reference type affects the original data. This will help you understand reference types behavior in Solidity.
📋 What You'll Learn
Create a balances array with initial values
Create a tempBalances variable to hold a reference to balances
Write a function to modify tempBalances and observe changes in balances
Print the balances before and after modification
💡 Why This Matters
🌍 Real World
Understanding reference types is important when writing smart contracts that manage data efficiently and correctly on the blockchain.
💼 Career
Blockchain developers must know how data is stored and modified in smart contracts to avoid bugs and security issues.
Progress0 / 4 steps
1
Create the initial balances array
Create a public uint[] array called balances with these exact values: 100, 200, and 300.
Blockchain / Solidity
Need a hint?

Use uint[] public balances = [100, 200, 300]; inside the contract.

2
Create a reference variable to balances
Inside the contract, create a public function called setTempBalances that declares a local uint[] storage variable named tempBalances and assign it to balances.
Blockchain / Solidity
Need a hint?

Use uint[] storage tempBalances = balances; inside the function.

3
Modify the reference variable
Inside the setTempBalances function, add code to change the first element of tempBalances to 999.
Blockchain / Solidity
Need a hint?

Use tempBalances[0] = 999; to modify the first element.

4
Check the updated balances
Add a public view function called getBalances that returns the balances array. This will show the updated balances after calling setTempBalances.
Blockchain / Solidity
Need a hint?

Use function getBalances() public view returns (uint[] memory) { return balances; }.