In this article, we are going to understand the variables and their usage in Solidity. In our previous article
Solidity For Beginners, we’ve seen the basic introduction of the Remix IDE and deployed our first smart contract. If you’re new here, I would encourage you to read the previous article first.
The latest stable release of Solidity is 0.5.0, and that has breaking changes. There is a strong possibility that the old code would not run with the latest version. With this note, let’s get started with variables and types.
Access Modifier
Access Modifiers are the keywords used to specify the declared accessibility of a function or a type. There are four access modifiers available in Solidity.
Public
The Public element can be inherited and can be accessed by external elements. All can access a public element.
Private
The Private element doesn’t get inherited and can't be accessed by external elements. It can be accessed from the current contract instance only.
Internal
The Internal element can be inherited but can’t be accessed by external elements. Only the base contract and derived contract can access internal element.
External
The External element can’t be inherited but it can be accessed by external elements. Current contract instance can’t access external element, it can be accessed externally only.
Variable Declaration
Variable declaration in Solidity is a bit different; you have to specify the type (data type) first, followed with an access modifier, and the variable name. If you would not specify any access modifier, it will be considered as a private.
Structure
- <type> <access modifier> <variable name> ;
Example
There are mainly two types of variables available in Solidity - State variables and the Local variables. We consider them as Global variables and Local variables just as any other language. Though, there are some differences.
State variable
State variables store the value permanently in the contract storage. Consider that you’re using C# or other languages and you want to store user information for a long time, what would you do? Connect your application with a database server and then save that information to the DB. However, in Solidity, no need to make a connection, you can easily store your data permanently with just the use of state variables.
Each method has its own scope, and the State variables should define outside of the scope of any defined functions.
Local Variable
The context of a local variable is within the function, and it cannot be accessed outside. Usually, these variables are used to hold temporary values for processing or calculating something. “temp” in the upper screen is a local variable, which we cannot use outside of “addition” function.
Furthermore, there is a difference in a storage location between state and local variables that we’ll cover in a separate article.
Types
Solidity is a statically typed language, which means the type of each variable needs to be specified. Declared types have some default values, typically called “zero-state”. For instance, the default value for bool is false.
There is no concept of null or undefined in Solidity! Sounds great, no need to handle null reference exception here :D
Same as other languages, there are two kinds of types in Solidity: value types and reference types.
Value Type
These variables are passed by value, it means that they are copied when they are used either in assignment or in a function argument. Here we’ll see basic value types.
Booleans
Booleans have possible values as true or false, and can be used with a different operator for conditional checking.
Integers
It stores the values in a rage of 8 bits to 256 bits. Un-signed integer holds positive values and signed integer holds positive as well as negative values. int and uint are the aliases for int256 and uint 256, respectively. Best practices for integers is to specify the bits value while you declare them, so, you would use minimum space and the cost of storing would be lesser. Thus, use uint8 or uint16 instead of using int (uint 256) always.
Fixed Point Numbers
As per Solidity docs,
Fixed point numbers are not fully supported by Solidity yet. They can be declared, but cannot be assigned to or from.
However, you can use floating point number for calculation, but the value coming out of from the calculation should be an integer value.
Now let’s do a slight change here,
Here, Solidity compiler got confused, the expression 1.5 + num has no proper type, therefore compiler doesn’t accept that code and threw an error.
Bytes and Strings
Bytes are used to store a fixed size character set. Basically, the length of bytes is from 1 to 32. If you want to store more than that, you can use string, which has dynamic length. An advantage of bytes is, bytes1 to bytes32 use less gas, so they are better to use when you know how long data you have to store.
As the screen above, bytes are in hex format if you convert that hex, it will give you the desired output. If you wonder about keyword “memory”, never mind, we’ll cover up that in a data location part.
Enums
Enum is a way to create user-defined types, it used to assign names to integral constants, which makes the contract more readable and maintainable. Options in enum are represented by subsequent unsigned integer values starting from 0.
- pragma solidity ^ 0.5.0;
-
- contract Types {
-
- enum Week { Mon, Tue, Wed, Thur, Fri, Sat, Sun }
-
- function getFirstEnum() public pure returns(Week) {
- return Week.Mon;
- }
- }
-
-
We can also set default value;
- pragma solidity ^0.5.0;
-
- contract Types {
- enum Week { Mon, Tue, Wed, Thur, Fri, Sat, Sun }
- Week week;
- Week constant defaultValue = Week.Sat;
-
- function getDefaultValue() public pure returns(Week) {
- return defaultValue;
- }
- }
-
-
Summary
In this article, we learned about variables in Solidity. We also covered up state, local variables, and different types with a value type. Then, we tried to understand Booleans, Integers, bytes, string, and an enum. There are other value types available such as Address and Function that we’ll learn in our next article
Function And Address In Solidity.