🎓 A1Learner
🔍
0 of 50 lessons completed

Solidity Get Started

Getting started with Solidity means writing a first contract, compiling it, and trying it on a practice chain. This lesson builds one small contract that stores a number and reads it back.

Store a number

The store function receives a number as a parameter. It saves that number in the contract's storedNumber variable.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;

contract StorageBox {
    uint256 storedNumber;

    function store(uint256 newNumber) public {
        storedNumber = newNumber;
    }

    function retrieve() public returns (uint256) {
        return storedNumber;
    }
}

Read the number back

The retrieve function returns the saved number to its caller. Before anything is stored, it returns 0.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;

contract StorageBox {
    uint256 storedNumber;

    function store(uint256 newNumber) public {
        storedNumber = newNumber;
    }

    function retrieve() public returns (uint256) {
        return storedNumber;
    }
}

A new store replaces the old one

Write the contract in Remix, compile it, and deploy it to the practice chain. Then call store and retrieve from the deployed contract panel to see the values change.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;

contract StorageBox {
    uint256 storedNumber;

    function store(uint256 newNumber) public {
        storedNumber = newNumber;
    }

    function retrieve() public returns (uint256) {
        return storedNumber;
    }
}

Example

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;

contract StorageBox {
    uint256 storedNumber;

    function store(uint256 newNumber) public {
        storedNumber = newNumber;
    }

    function retrieve() public returns (uint256) {
        return storedNumber;
    }
}

Key points

  • Remix: an online editor where you can write and try Solidity in a browser.
  • Compile: to turn Solidity code into instructions a blockchain can run.
  • Practice chain: a pretend blockchain inside Remix for safe experiments.
  • Parameter: a value a function receives when it is called.
  • Return: a value a function hands back to its caller.
  • Store and retrieve: one function saves a number, and another function reads it back.

In this example, StorageBox declares one state variable named storedNumber, which starts at 0. The store function takes a number as a parameter and saves it in storedNumber. The retrieve function returns the saved number, so store(42) followed by retrieve() gives 42.

Note

Calling store changes the contract, while calling retrieve only reads it.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Remix IDE

Exercise

After calling store(42) on the StorageBox contract, what does retrieve() return?