Global Variable

Global variables are special variables provided by the Solidity language. They are available throughout the contract and provide information about the blockchain, transaction, and execution context.

Examples of Global Variables:

  • block.timestamp (current block timestamp)
  • msg.sender (address of the sender of the current function call)
  • msg.value (amount of ether sent in the current function call)

Below is the Solidity program to implement the Global variable:

Solidity




// Solidity program to implement 
// Global variable
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
  
contract GlobalVariableExample {
  event SenderAndValue(address sender, uint value);
  function getSenderAndValue() public payable 
  {
    address sender = msg.sender;
    uint value = msg.value;
    emit SenderAndValue(sender, value);
  }
}


Output: Calling the getSenderAndValue function will return the sender’s address and the ether value sent in the current function call.

 

Solidity – Variable Scope

Variable scope is an essential concept in Solidity, the programming language for Ethereum smart contracts. Solidity has various types of variables with different scopes, such as local, state, and global variables.

Similar Reads

Local Variable

Local variables are declared within a function and are only accessible within that function. Their scope is limited, and their lifetime ends when the function execution is completed....

State Variable

...

Global Variable

State variables are declared at the contract level and represent the contract’s state on the blockchain. They are stored on the Ethereum network and are accessible within the entire contract....

Variable Scope

...