Structs, Arrays & Functions

Structs in Solidity

Sometimes you have to store complex information such as details of customers for your business. For this kind of storage using basic data types will make storage and retrieval of data difficult. So we use structs to create complex data types.

Code:

struct customer {

uint id;

string name;

}

Arrays in Solidity

Arrays are used to store a collection of information. There are two types of arrays in solidity.

  1. Fixed-sized arrays for which length is known
  2. Dynamically-sized array for which the length may be unknown

Syntax:

data_type[ ] array_name;

Code

string[2] cars;  //Fixed sized array where we can only store information of two cars.

string[ ] cars;  //Dynamically sized array where we can store information of any number of cars..

Note:

You can declare an array as public and solidity will automatically create a getter method for it. The getter function is used to access the array variables directly and not for retrieving the array itself. Now other contracts will be able to read from but not write to this array.

Code:

string[ ] public cars;

Functions in Solidity

Functions are "self contained" modules of code that accomplish a specific task. Functions usually "take in" data, process it, and "return" a result. Once a function is written, it can be used over and over and over again. Functions can be "called" from the inside of other functions.

Code:

data_type function_name (data_type storage_type parameter) accessibility {

}

Note:

data_type: uint, string, struct etc…

storage_type: storage, memory

parameter: name listed in the function definition

accessibility: public , private, internal, external

Storage types (Storage & Memory)
Storage is where all the contract state variables reside. Every contract has its own storage and it is persistent between function calls.
Memory is used to hold temporary values. It is erased between (external) function calls.

Code:

function cars (string, memory _name) public {

}

Complex Arrays in Solidity

We can create complex arrays like array of structs in solidity.

Code:

struct emp_detail {

uint id;

string name;

}

Complex Array:

emp_detial[ ] public emp_details;

Note: Data type of emp_details is emp_detail which is a struct created by us.

Function types in Solidity

Function types come in four flavors — internal, external, public and private functions:

Internal functions can only be called inside the current contract (more specifically, inside the current code unit, which also includes internal library functions and inherited functions) because they cannot be executed outside of the context of the current contract.

External functions consist of an address and a function signature and they can be passed via and returned from external function calls. Can be called from outside, can’t be call from inside (functions from same contract, functions from inherited contracts).

Private functions can only be called from inside the contract, even the inherited contracts can’t call them.

Public functions can be called from anywhere.