#100DaysOfSolidity Exploring Reading and Writing to State Variables in Solidity

#100DaysOfSolidity Series 007 “State Variables”

Solidity Academy
4 min readJul 1, 2023

Solidity, the programming language used for developing smart contracts on blockchain platforms like Ethereum, offers various features and functionalities. In this article, we will dive into the topic of reading and writing to state variables in Solidity. State variables are essential for storing and accessing data within smart contracts. We’ll explore how to update state variables by sending transactions and how to read their values without incurring any transaction fees.

#100DaysOfSolidity Exploring Reading and Writing to State Variables in Solidity

Understanding State Variables

State variables in Solidity are variables declared within a contract that retain their values across function calls. They represent the contract’s storage and enable the persistent storage of data. Let’s take a closer look at the following code snippet, which showcases the usage of state variables:

/// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
contract SimpleStorage {
// State variable to store a number
uint public num;
// You need to send a transaction to write to a state variable.
function set(uint _num) public {
num = _num;
}
// You can read from a state variable without sending a transaction.
function get() public view returns (uint) {
return num;
}
}

In the above code, we have the `SimpleStorage` contract with a state variable named `num`. It is declared as a public unsigned integer (`uint public`), allowing its value to be accessed from outside the contract. Additionally, the contract includes a `set` function for updating the value of `num` and a `get` function for reading its value.

Writing to a State Variable

To update the value of a state variable, we need to send a transaction to the blockchain. In the `SimpleStorage` contract, the `set` function is responsible for updating the `num` variable. Let’s examine it further:

```solidity
function set(uint _num) public {
num = _num;
}
```

The `set` function takes an input parameter `_num` of type `uint` and assigns its value to the state variable `num`. By invoking the `set` function and providing a desired value, the state variable will be updated accordingly. It’s important to note that writing to a state variable requires sending a transaction, which typically incurs a small fee referred to as a transaction fee or gas fee.

Reading from a State Variable

Unlike writing to a state variable, reading its value is free and does not require any transaction fees. In the `SimpleStorage` contract, the `get` function allows us to retrieve the current value of `num`:

```solidity
function get() public view returns (uint) {
return num;
}
```

The `get` function is declared as `public` and `view`. The `public` modifier enables external access to the function, while the `view` modifier ensures that the function does not modify the contract’s state. By calling the `get` function, we can retrieve the current value of `num` without incurring any transaction costs.

Conclusion

In this article, we explored the concept of reading and writing to state variables in Solidity. State variables serve as the persistent storage for smart contracts, allowing data to be stored and accessed throughout their execution. While writing to a state variable requires sending a transaction and incurring transaction fees, reading from a state variable is free of charge. It’s crucial to consider these aspects when designing and implementing smart contracts, ensuring efficient utilization of blockchain resources.

Sample Code

The complete code for the `SimpleStorage` contract is provided below:

/// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
contract SimpleStorage {
// State variable to
store a number
uint public num;
// You need to send a transaction to write to a state variable.
function set(uint _num) public {
num = _num;
}
// You can read from a state variable without sending a transaction.
function get() public view returns (uint) {
return num;
}
}

Remember to compile and deploy the contract on an Ethereum development network to interact with it.

Detailed Analysis of the Smart Contract

The provided `SimpleStorage` contract showcases the reading and writing of a state variable. Let’s take a closer look at its components:

1. State Variable:
— The contract includes a state variable named `num`, which is declared as a public unsigned integer (`uint public`).
— The `public` visibility modifier automatically generates a getter function for `num`.
— By default, the initial value of `num` is set to 0.

2. `set` Function:
— The `set` function allows us to update the value of `num` by accepting a parameter `_num` of type `uint`.
— The `public` visibility modifier enables both internal and external access to the function.
— Inside the function, the value of `_num` is assigned to `num`, thereby updating its value.

3. `get` Function:
— The `get` function enables us to read the current value of `num`.
— The function is declared as `public` and `view`.
— By calling the `get` function, we can retrieve the value of `num`.

By utilizing the `set` function, you can update the value of `num`, while the `get` function allows you to retrieve the stored value without incurring any transaction fees.

📚👩‍💻 Solidity Learning Resources

📜🔐 Solidity’s SubStack

💡💼📝 Smart Contracts Made Simple

🆓🆓🆓 FREE books that are essential

Conclusion

Understanding how to read and write state variables in Solidity is essential for developing robust and efficient smart contracts. State variables enable the persistent storage of data on the blockchain. While writing to a state variable incurs transaction fees, reading from it is free. By leveraging state variables effectively, you can store and retrieve data, facilitating the development of powerful decentralized applications. Solidity offers a wide range of features and capabilities for building secure and transparent systems on various blockchain platforms. Keep exploring and experimenting to unlock the full potential of smart contract development!

Happy coding!

--

--

Solidity Academy

Your go-to resource for mastering Solidity programming. Learn smart contract development and blockchain integration in depth. https://heylink.me/solidity/