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.