Variables in Solidity

variables in solidity

3 types of variables exist in the Solidity programming language: 

state variables are stored on the blockchain, it makes the data persistent. The state variables are defined inside of a contract but outside of functions.

local variables exist only inside a function. Their scope is local and it's not possible to access them anywhere else.

global variables exist in the global scope and provide information about the blockchain. For example, the gas limit, the current block number, the block timestamp, or the address of the current caller.

state variables

The state variables are permanently stored on the blockchain. They are defined inside of a contract but outside of the contract's functions and they have a global scope. In the following example code the state variable myStateVariable is declared. // SPDX-License-Identifier: MIT pragma solidity ^0.8; // state variables in Solidity contract StateVariables { uint public myStateVariable; }

local variables

The local variables have a local scope and they are declared inside of functions. That means a local variable can not be accessed from outside of the function where it was declared. In the following example code the local variable myLocalVarible is declared and the value 123 is assigned to it. It exists in the local scope only. After the execution of the function, the data of local variables are gone. // SPDX-License-Identifier: MIT pragma solidity ^0.8; // local variables in Solidity contract LocalVariables { function exampleFunction() external pure { uint myLocalVariable = 123; } }

global variables

The global variables store data about the blockchain. To read global variables from within a function, the function has to be declared as a view function. In the example code below, we read the global variables: the address of the sender, the current block number and the block timestamp. // SPDX-License-Identifier: MIT pragma solidity ^0.8; // global variables in Solidity contract GlobalVariables { function exampleFunction() external view returns (address, uint, uint) { // address of sender of the message address sender = msg.sender; // current block number uint block_number = block.number; // current block timestamp uint timestamp = block.timestamp; return (sender, block_number, timestamp); } } The table gives an overview of global variables in Solidity.
global variable data type description
blockhash(uint block_number) bytes32 hash of the given block number
block.coinbase adress payable current block miner address
block.difficulty uint current block difficulty
block.number uint current block number
block.gaslimit uint current block gaslimit
block.timestamp uint current block timestamp (unix epoch timestamp)
gasleft() uint remaining gas
msg.data bytes calldata complete calldata
msg.sender address payable sender of message
msg.sig bytes4 first 4 bytes of calldata
msg.value uint wei sent with message
now() uint current block timestamp
tx.origin address payable sender of transaction
tx.gasprice uint gas price of transaction



Click to jump to section