DEV Community

Cover image for Solidity : Introduction

Posted on


Solidity : Introduction

In this module we will go over what Solidity is and the basic syntax of Solidity.


Solidity is a high-level object-oriented programming language to implement smart contracts which is targeted for Ethereum Virtual Machine(EVM). With solidity we can create self-executing contracts which governs the behavior of accounts within Ethereum State.

Getting Started

Structure of a Contract:

Here's a sample smart contract structure:

//SPDX-License-Identifier: MIT
pragma solidity^0.8.17

contract HelloWorld{
string public myString = "hello world";
Enter fullscreen mode Exit fullscreen mode

We need the license or it will give errors.
pragma solidity ^0.8.17 indicates that the version is greater than or equal to 0.8.17. We used a string varibale to write "Hello World" and here public menas having read access to the variable after deploying the contract.


There are 3 types of variable in Solidity: local, state and global variables.

Local Variables:

These variables are defined and used within a function and can only exist within the function. These functions are automatically created and destroyed when the function gets called and returned.

function add(uint x, uint y) public pure returns (uint) {

    uint music = x + y;
    return music;

Enter fullscreen mode Exit fullscreen mode

here, the variable music1 is a local variable and can only be accessed within the add function.

State Variables:

State variables are declared outside of the function to maintain the state of the smart contract and also stored in the contract storage. State variables are public by default and accessible by all functions within the contract.

contract MyContract {
    uint256 public totalSupply;
    mapping(address => uint256) public balances;
Enter fullscreen mode Exit fullscreen mode

here,totalSupply and balances are state variables.

Global Variables:

Global variables are injected to virtual machine during runtime and mostly contains information about the blockchain.

Values and References

Data types in solidity can be classified as two types.

  • values

  • references

Value Types

Value means that the data stores a value. Boolean stores true or false. Int stores : -1, 0, 1.


References data types don't store values, instead they store references where the actual value is. Array type of type references will store where actual array elements are stored.

Data Types:

Signed Integer: The int data types used to define positive or negative integers.

int public i = -5;
Enter fullscreen mode Exit fullscreen mode

Unsigned Integer : The uint data type are used to define unsingned(positive) integer. The number has to be greater than or equal to 0. No negative numbers.

uint public i = 327; 
//uint = uint256 0 to 1**256-1

Enter fullscreen mode Exit fullscreen mode


This data type is used to define true or false values.

bool public isValid = true;
Enter fullscreen mode Exit fullscreen mode


The address data type comes as 20 byte size to store ethereum address.

address public addr = 0xAE5679C84371d53c7e2BB2870f9d00e92D506E0c;
Enter fullscreen mode Exit fullscreen mode

The "bytes" keyword allows to define a byte array of any size.
We will use it during cryptographic hash function kechak256;

bytes32 myHash = keccak256("example data");

Enter fullscreen mode Exit fullscreen mode

Arrays: Arrays are collections of variable of the same type and can be declared and used.


The type of the array elements and the length of the array must be specified. There are two types of Arrays.

  • Dynamic
  • Fixed Size

Dynamic Array : The size of the array can be changed.

Fixed Array : The size of the array can't be changed.


In Solidity, functions are used to define the behavior of a smart contract. Functions are defined with the "function" keyword and can take input parameters and return values.

function add(uint x, uint y) public pure returns (uint) {

    uint music = x + y;
    return music;


Enter fullscreen mode Exit fullscreen mode

Mapping: In solidity, to store a collection of data we can either use Array or mapping. Mapping allows us to save the data/value at the key and later we can get the key. In other words, mapping is the fastest way to get any value or data.


Struct: When we need to use a bit more complicated data type with multiple properties, then we can use struct. It allows us to define a custom data type with a set of properties.


pragma solidity ^0.8.17
contract Structs {

    Struct Car {
        string model;
        uint year;
        address owner;
    Car Public car;

Enter fullscreen mode Exit fullscreen mode

Enum :

Boolean lets you pick two choices between true and false but what if you need to pick more choices. Then enum is a perfect data type for this.

pragma solidity ^0.8.17

contract Purchase {
    enum State {Created, Locked, Inactive}

Enter fullscreen mode Exit fullscreen mode

That's all for the very basic of solidity. We will explore advanced concepts next time.


Top comments (0)

Advice For Junior Developers

Advice from a career of 15+ years for new and beginner developers just getting started on their journey.