Variables Basics

Andrei Dumitrescu
A free video tutorial from Andrei Dumitrescu
DevOps Engineer and Professional Trainer
4.6 instructor rating • 8 courses • 30,139 students

Learn more from the full course

Master Ethereum & Solidity Programming | Ethereum Blockchain

In-Depth Ethereum & Solidity Programming. A step-by-step approach to ERC20 Token, ICO, Auction, FundRaising,Lottery,IPFS

09:33:03 of on-demand video • Updated March 2021

  • Understand the Ethereum Blockchain Platform (accounts, addresses, ether, gas, opcodes, transactions, blocks, mining, etc)
  • Have a very good understanding of Solidity Contract Programming Language (syntax and concepts like state variables, storage, functions, visibility specifiers, mappings, structs, enums, etc)
  • Understand concepts like Events, Function Modifiers, Contract Inheritance, Abstract Contracts & Interfaces, etc
  • Design, create and deploy a fully-compliant ERC20 Token and launch an Initial Coin Offering (ICO)
  • Design, develop and deploy a smart contract for a decentralized Lottery
  • Design, develop and deploy a smart contract for a decentralized Auction Platform
  • Understand good practices for developing Ethereum Smart Contracts and see real-world examples to comprehend what is the advantage of Blockchain Technologies.
  • Design, develop and deploy a smart contract for a decentralized FundRaising Platform
  • Use geth and Set Up a Private Ethereum Blockchain
  • Understand what is InterPlanetary File System (IPFS) and how it works. Installing and Running an IPFS Node on Linux or Windows
English Hello everybody! In this lecture we are going to talk about variables. First of all we must know that Solidity is statically typed programming language. That means that for every variable we define we must specify its type. This is in contrast with other programming languages like PhP or Python where the variable type is deduced at runtime. This is not valid in Solidity; we must specify its type at declaration. OK then we have 2 types of variables we have state variables and local variables; state variables are permanently stored in contract storage; storage is not dynamically allocated. What does it mean? Let's take an example. I have a simple contract here named property and I'm going to declare a state variable, Like let's say a string variable - this is the variable type which is mandatory to be specified. After that optionally we can specify its visibility - for example public. We'll talk more about the visibility of variables and functions in a special, another chapter. So string public location this is a storage variable; let's declare another storage variable for example int public the same visibility and let's say price We have 2 storage variables. If I deploy this contract, this contract will have only two state variables. There is no possibility let's say to add another variable to say okay want this contract to have another variable named owner; it's not possible. All variables must be defined before deployment. This is what it means when I say storage is not dynamically allocated. OK then they must be declared at the contract level; this is contract level; after the name of the contract we declare the state variables for this contract. They must be initialized at declaration using a constructor or after contract deployment by calling setter functions. For example if I want to modify the value of the location variable; let's say location equals London This is not permitted in Solidity as you can see there is an error. This is not permitted. I can't define the variable after its declaration. This is possible in other programming languages like Python for example. Here this is not allowed. What can you do? So first of all I write this is not permitted in solidity. If I want to modify the value of this variable I can create a special function named setter; for example function setProperty This function must have at least one argument. Let's say a string argument named _location; this is another variable. This is why I call it _location. Public - public means that this function can be called from another application, from outside of this contract. For example I can have a web application that calls this function. This means public and now location = _location. When this function will be called - of course having one argument- this function will modify my storage variable named location. This is one possibility to modify a storage variable. Another possibility is to specify the value of the variable at declaration time. For example I can say here I can write Location=Paris Let's try to deploy this contract by pressing on deploy button; I've deployed the contract using the in-memory block chain. OK my contract received an address and now I have my two vriables- location and price- Location=Paris because when I declared the variable I specified the value of string Paris and price has the value of 0 ; an integer variable is automatically initialized with zero - if I don't specify another value of course - if I want to modify the value of location varible I can simply call this function. Here I have a button - setProperty - and I can write here let's say Amsterdam! OK. And now location; the new location is Amsterdam. So my function, my setter function modified the varible. Let's come back here. We'll have another lecture about constructors so we'll discuss more them. They are expensive to be used. They cost gas . OK, what does it mean? It means that this variable will be saved on the Ethereum block chain. If we save these variable on the block chain we must pay for it. So this variable costs gas; this variable costs also gas. So all storage variables cost gas because they are saved on the Ethereum block chain; What is a local variable? A local variable is declared at function level. What does it mean? For example inside this function I can declare, I can create another variable like say simply int a this is a local variable because it's declared inside the function. Then if using memory keyword and the local variable is an array or a struct, they are allocated at runtime, memory keyword can't be used at contract level. I want to explain a little bit more here. Let's create a new variable, a dynamic array variable of type integer. So int let's say simply X Okay my X variable is a dynamic array; being a dynamic array it will automatically be saved on storage like location and like price because dynamic and fixed arrays struct and mappings are all saved in storage. If I use the memory keyword here this variable, even if it's a dynamic array, will be allocated at runtime and it will be allocated in memory OK. This is what this keyword does. Then then let's see where does Ethereum virtual maschine save data. The Ethereum virtual machine has 3 areas where it saves data. It has storage, memory and stack ; storage is where state variables are saved. Storage is persistent and expensive. It's like a computer hard drive. For example if I deploy this contact on the Ethereum block chain the value of these variable will be there forever in the future. If I call this function get location or simply location because the variable is public I will get the the value of this variable. This means persistent ; expensive because as I have already said it costs gas. Another area where Ethereum virtual machine holds data is memory. There we find temporary values. It's free to be used and it doesn't cost gas. It's like a computer RAM memory. And then we have the stack - it holds function variables and it's free to be used. Let's come to a conclusion. First state variables are always saved in storage; function arguments, function arguments for example _location is a function argument , function arguments are local and are stored in memory. Then variables defined inside functions are saved in storage if they are struct array or mappings or they are saved on thestack if they are of any other type like example an integer. Let's clarify this. I erase this keyword; So this is a dynamic array, being a dynamic array this is saved in storage. This is an integer variable; being an integer variable means that these variable will be saved on the stack so saved on the stack. OK. These variables are local. So I write here local variable and the location of this variable is also a local variable and is saved in memory. I can comment here _location is local and saved in memory. Let's delete this contract deployment. I deployed the contract one more time. So location is initialized with value of string Paris price is automatically initialized with 0 and if I want to modify the value of location I can simply call this function I called the function and now the new location is Madrid ; If I want to modify the value of variable named Price. I can't d it; There is no possibility because the interface doesn't have such a method, such a function. There is no function like set price so price can be modified by anyone after the contract is deployed.