#100DaysOfSolidity 📝 Solidity Variables: Exploring Local, State, and Global Variables

#100DaysOfSolidity Series 004 “Variables”

Solidity Academy
4 min readJul 1, 2023

Welcome to the fourth article in the #100DaysOfSolidity series! In this post, we will delve into the world of variables in Solidity, the programming language for developing smart contracts on the Ethereum blockchain. Variables play a vital role in storing and manipulating data within smart contracts. Solidity offers three types of variables: local, state, and global. Understanding the distinctions between these variable types is crucial for writing efficient and secure smart contracts.

📚👩‍💻 Solidity Learning Resources

📜🔐 Solidity’s SubStack

💡💼📝 Smart Contracts Made Simple

🆓🆓🆓 FREE books that are essential

Let’s explore each type in detail and gain a comprehensive understanding of their significance in Solidity development.

1️⃣ Local Variables:

Local variables are declared within functions and are not stored on the blockchain. They have a temporary lifespan and are accessible only within the scope of the function in which they are declared. Local variables are useful for storing intermediate results or data required for computation within a specific function. Let’s take a look at an example:

pragma solidity ^0.8.17;
contract Variables {
function doSomething() public {
uint i = 456;
// Rest of the function code…
}
}

In the above example, the variable `i` is a local variable. It is assigned the value 456 within the function `doSomething()`. Once the execution of the function is complete, the variable `i` is destroyed, and its value is no longer accessible.

2️⃣ State Variables:

State variables are declared outside of functions and are stored on the blockchain. These variables persist between function calls and are associated with the contract instance. State variables are suitable for storing contract-specific data that needs to be accessed and modified by multiple functions within the contract. Let’s consider an example:

pragma solidity ^0.8.17;
contract Variables {
string public text = "Hello";
uint public num = 123;
// Rest of the contract code…
}

In the above example, the variables `text` and `num` are state variables. The `public` visibility modifier generates automatic getter functions, enabling access to their values from outside the contract. These variables are stored on the blockchain and can be read or modified by other contracts or external entities.

3️⃣ Global Variables:

Global variables in Solidity provide essential information about the blockchain and the current transaction. These variables are available to all functions within a contract and offer crucial context for contract execution. Let’s explore two commonly used global variables:

- `block.timestamp`: This variable returns the current timestamp (in seconds) of the block in which the contract is being executed. It enables contracts to incorporate time-based logic or record the occurrence of specific events.

- `msg.sender`: This variable represents the address of the account (or contract) that called the current function. It can be used to verify the identity of the caller or perform access control checks.

Here’s an example that demonstrates the usage of global variables:

pragma solidity ^0.8.17;
contract Variables {
function doSomething() public {
uint timestamp = block.timestamp;
address sender = msg.sender;
// Rest of the function code…
}
}

In the above example, we retrieve the current timestamp using `block.timestamp` and store it in the `timestamp` variable. Similarly, we obtain the caller’s address using `msg.sender` and assign it to the `sender` variable. These global variables provide valuable information for smart contract development and enable various functionalities.

🔍 Detailed Analysis of the Provided Smart Contract:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
contract Variables {
string public text = "Hello";
uint
public num = 123;
function doSomething() public {
uint i = 456;
uint timestamp = block.timestamp;
address sender = msg.sender;
// Rest of the function code…
}
}

In the provided smart contract, we have two state variables: `text`, which is of type `string` and initialized with the value “Hello,” and `num`, which is of type `uint` and initialized with the value 123. These variables are stored on the blockchain and can be accessed using their respective getter functions (`text()` and `num()`).

The `doSomething()` function is a public function that showcases the usage of local and global variables. It declares a local variable `i` of type `uint` and assigns it the value 456. This local variable exists only within the scope of the `doSomething()` function.

Additionally, within the `doSomething()` function, we retrieve the current block timestamp using the global variable `block.timestamp` and assign it to the local variable `timestamp`. We also obtain the address of the caller using the global variable `msg.sender` and assign it to the local variable `sender`. These global variables provide crucial information about the blockchain and the transaction context.

#100DaysOfSolidity Series 004 “Variables”

Conclusion:

In this article, we explored the three types of variables in Solidity: local, state, and global. Local variables are temporary and exist only within the function scope, while state variables are stored on the blockchain and persist between function calls. Global variables provide important information about the blockchain and the transaction context. Understanding and appropriately using these variable types are essential for developing efficient and secure smart contracts. By leveraging the power of variables, you can store and manipulate data within your contracts to create powerful decentralized applications.

🎉 Congratulations on completing the fourth article of the #100DaysOfSolidity series! Stay tuned for more exciting topics in the upcoming articles. 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/