top of page

Master Solidity State variables, Data types and Functions

  • Writer: Avi
    Avi
  • Nov 18, 2020
  • 5 min read

Why should you read this blog? what's in it for you?

After reading this post you will be confident enough to write basic to advance smart contracts using solidity.


Contents:

  1. Data types,

  2. State variable visibility types,

  3. Functions

Data types: these are of two types: Value types and Reference types.

  1. Value Types: because variables of these types will always be passed by value, i.e. they are always copied when they are used as function arguments or in assignments. 1.1. Boolean: possible values true or false e.g. bool isAllowed = true; 1.2. Integer: Signed (int) negative and positive integers or Unsigned (unit) only positive integers. we can limit the numbers passed to the variable in solidity from uint8 to uint256, in steps of 8 i.e. uint8, uint16, uint32 ..... uint256 note: uint and int are aliases for uint256 and int256, respectively. e.g. >uint num = 1; or int num = -2; >uint8=32; here uint8 will take all values from 0 to 255, as we can represent 0 to 255 in 8 bits. 1.3. String: are written with either double or single-quotes ("foo" or 'bar') note: Data location must be "storage", "memory" or "calldata" e.g. string memory name = "avi"; We could also store strings using bytes; i.e. bytes3 name = "avi"; important here if we try to assign name ="avin" this will throw an error as the bytes that we define can only take 3 characters. Bytes is a good way to save memory in Solidity if we know how many characters we want the user to enter. 1.4. Address: Holds a 20 byte value (size of an Ethereum address). e.g. address node1 = 0x123; 1.5. Fixed-size byte arrays: a collection of values where array size is fixed. e.g: dataType[] <arrayName> = new dataType[](size); or dataType[size] <array Name> = [value1, value2,...]; unit[] num = new unit[](2); num.add(1); num.add(2); or unit[3] num = [23,1,76]; note: solidity lets us define two characteristics of an array, one size of the array and two accepted range of values. uint8[3] num = [255,23,0]; after defining uint8 we limited the value range to 0-255, uint8[3] num = [256,23,0]; will throw an error as 256 is not a 8bit binary number.



2. Reference types: Complex types, i.e. types which do not always fit into 256 bits and we have to think about whether we want them to be stored in memory (which is not persisting) or storage (where the state variables are held).


2.1. Dynamically-sized byte array: a collection of values where array size is dynamic. e.g. dataType[] <arrayName>; string[] name; or unit[] name; add to array: name.push("avi"); name.push("John"); retrieve from array: string memory personName = name[0];

2.2. Mapping: can be seen as hash tables and declared as mapping(_KeyType=>_ValueType). KeyType can be a datatype: any type except for a mapping, a dynamically sized array, a contract, an enum, and a struct. the real power of mapping is unleashed when paired with a "struct" explained later in 2.3. e.g. mapping (string => unit) public <mapping name> mapping (string => unit) public student; add to mapping: student["avi"]=2; student["john"]=3; retrieve from mapping: unit rollNumber = student["avi"]; // this will assign rollNumber = 2; note: mapping is not iterative, so let's say if we have a mapping of 100 different key=>values and we wish to find the corresponding value for the 50th Key we should always know the key. This is a drawback as it's not possible for someone to remember all the keys. Solution: we could use an array to store all the keys and then we could iterate over the array to find the key and then use mapping to find the corresponding value. e.g. mapping (string => unit) public student; string[] studentName; add to mapping: student["avi"]=2; student["john"]=3; at the same time store the key in array: studentName.push("avi");


Retrieve from mapping with the help of array: unit studentRollNumber = student[studentName[0]]; //we could use a for loop to iterate over all the elements in the array and find any corresponding key-value we would like. 2.3. Struct: a way to define new types in the form of structs, it is a collection of different data types, which can be seen as an object in JavaScript. e.g. struct studentDetails { unit index; string place; unit[] marks;

};


Now is the time to unleash the real power of mapping by using struct. e.g. mapping (string => studentDetails) public student; adding values: student["avi"].index = 1; student["avi"].place = "Bangalore"; student["avi"].marks[0]=100; student["avi"].marks[1]=99; accessing values: uint studentRollNum = student["avi"].index;

string studentLocation = student["avi"].place; deleting: delete student["avi"]; note: we have only covered the most important Data Types, if you wish to learn about all the datatypes please refer to the official solidity documentation. I understand the implementation of mapping and struct could be overwhelming at first If you are still baffled don't worry I will explain the usage of all the datatypes with the help of a smart contract.


2. State variable visibility types:

State variables are variables that are defined inside a smart contract. A state variable can have the following visibility types:

  • Public: If a state variable is defined as public, then it can be accessed from outside the contract namespace. In other words, it can be accessed from any other contract on the network.

  • Private: If a state variable is defined as private, then it can be accessed only inside the smart contract in which it is defined. Even if you import this contract in some other contract, the state variable with private visibility cannot be used from the contract inheriting the parent contract.

  • Internal: A variable with internal visibility can only be used from the contract in which it is defined or the contract inheriting the properties of the parent contract. So if a contract 'A' inherits a contract 'B', then it can use the variables in 'B' with the internal visibility type but not with the private visibility type. The variables with public visibility can be used by any contract on the network.

A state variable is defined as <visibility type> <data type> <variable name>;

e.g. public uint votingCount;

internal mapping(address=>bytes32) sampleMapping;


3. Functions:

syntax: function <function name>(Input<arguments>)<visibility> <function type> <modifiers> returns(){ } 3.1. Visibility types of Function: Apart from the three visibility explained above, functions have 1 more extra visibility type "External". External visibility: If a function is defined as external visibility then it can't be used within the contract, which means it can't be called from any other function in the contract. This function can only be called from an external call.



3.2. Function types:

  1. View: this is a read-only function to read from the blockchain.

  2. Pure: this is a sort of helper function that doesn't connect to the blockchain and runs internally

  3. Payable: this is a function which could accept ETHERS as input (no other function can accept ETHERS as input)

  4. Fallback: If the incoming request fails to match with any of the defined functions in the contract then at last the control goes to the fallback function. There could be one and only one FALLBACK function in each contract.


3.3. Function Modifiers:


These are special function that runs before the main function to check a set of requirements or conditions.

In order for the main function to run the required conditions or requirements needs to be satisfied

syntax : modifier abc(input arguments)

{

_;

}

note: every modifier need to end with _; to transfer control to the main function

we can have multiple modifiers for 1 function.

e.g.

ree


Comments


Contact

  • Black Facebook Icon
  • Black Twitter Icon
  • Black Instagram Icon
  • Black YouTube Icon

© 2021 by CyberFit Chronicles

Thanks for submitting!

bottom of page