Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Day 4 - Variables and Scopes
Vedant Chainani
Vedant Chainani

Posted on • Edited on

     

Day 4 - Variables and Scopes

GitHub logo Envoy-VC / 30-Days-of-Solidity

30 Days of Solidity step-by-step guide to learn Smart Contract Development.

This is Day4 of30 in Solidity Series
Today I Learned About Variables and Scopes in Solidity.

Variables

Solidity supports three types of variables.

  • State Variables − Variables whose values are permanently stored in a contract storage.

  • Local Variables − Variables whose values are present till function is executing.

  • Global Variables − Special variables exists in the global namespace used to get information about the blockchain.


While naming your variables in Solidity, keep the following rules in mind.

  • You should not use any of the Solidity reserved keywords as a variable name. For example, break or boolean variable names are not valid.
  • Solidity variable names should not start with a numeral (0-9). They must begin with a letter or an underscore character. For example, 123test is an invalid variable name but _123test is a valid one.
  • Solidity variable names are case-sensitive. For example, Name and name are two different variables.

State Variable

Variables whose values are permanently stored in a contract storage.

pragmasolidity^0.8.6;contractSolidityTest{uintstoredData;// State variableconstructor()public{storedData=10;// Using State variable}}
Enter fullscreen modeExit fullscreen mode

Local Variable

Variables whose values are available only within a function where it is defined. Function parameters are always local to that function.

pragmasolidity^0.8.6;contractSolidityTest{uintstoredData;// State variableconstructor()public{StoredData=10;}functiongetResult()publicviewreturns(uint){uinta=1;// local variableuintb=2;uintResult=a+b;returnResult;//access the local variable}}
Enter fullscreen modeExit fullscreen mode

Global Variables

These are special variables which exist in global workspace and provide information about the blockchain and transaction properties.

NameReturns
blockhash(uint blockNumber) returns (bytes32)Hash of the given block - only works for 256 most recent, excluding current, blocks
block.coinbase (address payable)Current block miner's address
block.difficulty (uint)Current block difficulty
block.gaslimit (uint)Current block gaslimit

More on Global Variableshere


Variable Scopes

Scope of local variables is limited to function in which they are defined but State variables can have three types of scopes.

  • Public − Public state variables can be accessed internally as well as via messages. For a public state variable, an automatic getter function is generated.
  • Internal − Internal state variables can be accessed only internally from the current contract or contract deriving from it without using this.
  • Private − Private state variables can be accessed only internally from the current contract they are defined not in the derived contract from it.

eg-

pragmasolidity^0.8.6;contractC{uintpublicdata=30;uintinternaliData=10;functionx()publicreturns(uint){data=3;// internal accessreturndata;}}contractCaller{Cc=newC();functionf()publicviewreturns(uint){returnc.data();//external access}}contractDisC{functiony()publicreturns(uint){iData=3;// internal accessreturniData;}functiongetResult()publicviewreturns(uint){uinta=1;// local variableuintb=2;uintresult=a+b;returnstoredData;//access the state variable}}
Enter fullscreen modeExit fullscreen mode

GitHub logo Envoy-VC / 30-Days-of-Solidity

30 Days of Solidity step-by-step guide to learn Smart Contract Development.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Web3 Technical Writer ✍️ | ⛓️ Blockchain Enthusiast
  • Location
    India
  • Joined

More fromVedant Chainani

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp