This article takes you through the three variables in Ethereum smart contracts


theme: cyanosis

highlight: an-old-hope

This article is participating in the "Golden Stone Project. Share the 60,000 cash prize"

what is a variable

In computer languages, variables are simple tools for keeping track of almost any type of information. It can save the data entered by the user when the program is running, the results of specific operations, and a piece of data to be displayed on the form, etc.

Three variables in smart contracts

  • State variables – variables whose values ​​are permanently stored in the contract’s storage space.
  • Local variable – A variable whose value is valid only during the execution of the function. After the function exits, the variable is invalid .
  • Global variables – special variables stored in the global namespace and used to obtain blockchain-related information.
  • Note: Solidity is a statically typed language, which means that variable types need to be specified during declaration.

Why use variables

Solidity is a programming language, and programming can be understood as realizing functions by operating and storing variables. Therefore , without variables, there are no objects that can be used to operate , and variables can assign each piece of data to be used in the program to a Names (keep them short and meaningful), so they are very useful and necessary. A very simple example: a variable is like a dyer, whatever data goes in will be dyed into a certain color.

How to use variables

In solidity, we need to show how to use three variables.

State variables

Variable values ​​are permanently stored in the contract storage space. If the following contract is successfully deployed to the Ethereum mainnet, then the state variable storedData will be permanently stored on the Ethereum mainnet . This is almost the same as other programming languages. solidity pragma solidity ^0.6.0; contract SolidityTest { uint storedData; // 定义状态变量 constructor() public { //函数构造器,一个合约只能声明一个 storedData = 10; // 使用状态变量,给其赋值数字10 } } Note: As can be seen from the above code, we generally declare state variables outside the function body, and generally declare them collectively below the creation contract. When we create a function and need to declare variables, we usually put the state variable declaration at the top, together with the state variables of other functions.

local variables

The variable value is only valid during the execution of the function. After the function exits, the variable is invalid. Function parameters are local variables. solidity pragma solidity ^0.6.0; contract SolidityTest { uint storedData; // 定义状态变量 constructor() public { storedData = 10; // 使用状态变量,给其赋值数字10 } function getResult() public view returns(uint){ uint a = 1; // 定义局部变量 uint b = 2; uint result = a + b; return result; // 访问局部变量 } } Note: After we deploy the contract, the local variables a and b will not be permanently stored on the Ethereum mainnet.

global variables

Global variables are the most distinctive variables of solitity and the ones that test our memory the most. These variables are special variables that exist in the global workspace and provide information about the blockchain and transaction attributes . This is the meaning of some global variables I intercepted from the Chinese document.

image.png solidity pragma solidity ^0.6.0; contract SolidityTest { uint storedData; // 定义状态变量 constructor() public { storedData = 10; // 使用状态变量,给其赋值数字10 } function getResult() public view returns(uint){ uint a = 1; // 定义局部变量 uint b = 2; uint result = a + b; return result; // 访问局部变量 } function getAllVis()public view returns(){ return block.number; //返回当前区块号 } } - Note: For ease of understanding, these variables and functions can be understood as Solidity language level (native) APIs

Named variable rules

  1. Keywords cannot be used as variable names
  2. Start with a letter or underscore, not a number.
  3. It is case-sensitive. If the letters are the same, different case letters are equivalent to different variable names.

Guess you like

Origin blog.csdn.net/y943711797/article/details/132972203