0
0
Blockchain / Solidityprogramming~30 mins

Return values in Blockchain / Solidity - Mini Project: Build & Apply

Choose your learning style9 modes available
Return Values in Blockchain Smart Contracts
📖 Scenario: You are creating a simple blockchain smart contract to store and retrieve a user's favorite number. This contract will help users save their number and get it back when needed.
🎯 Goal: Build a smart contract that stores a number and returns it when requested using a function with a return value.
📋 What You'll Learn
Create a state variable to store a number
Create a function to set the number
Create a function to get the stored number with a return value
Print the returned number in the final step
💡 Why This Matters
🌍 Real World
Smart contracts often store data and return values to users or other contracts. Understanding return values is key to building interactive blockchain applications.
💼 Career
Blockchain developers must write functions that return data correctly to enable decentralized apps to work smoothly and securely.
Progress0 / 4 steps
1
Create a state variable to store a number
Create a public state variable called favoriteNumber of type uint to store the user's favorite number.
Blockchain / Solidity
Need a hint?

Use uint public favoriteNumber; inside the contract to create the variable.

2
Create a function to set the number
Add a public function called setNumber that takes a uint parameter named _number and sets favoriteNumber to this value.
Blockchain / Solidity
Need a hint?

Define function setNumber(uint _number) public and assign favoriteNumber = _number;.

3
Create a function to get the stored number with a return value
Add a public view function called getNumber that returns a uint and returns the value of favoriteNumber.
Blockchain / Solidity
Need a hint?

Use function getNumber() public view returns (uint) and return favoriteNumber.

4
Print the returned number
Write a script to create the contract, call setNumber(42), then call getNumber() and print the returned value.
Blockchain / Solidity
Need a hint?

Use a script to call setNumber(42) then getNumber() and print the result.