Lesson 6 of 23
In Progress

Basic Syntax and Data Types

As a programming language for writing smart contracts, Solidity has its own set of syntax and data types that you’ll need to understand in order to write effective contracts. In this article, we’ll provide an overview of the basic syntax and data types in Solidity, including examples of how to use them in your contracts.

Basic Syntax

Solidity is a statically-typed language, which means that variables must be declared with a specific type. Here is an example of a Solidity contract that defines a contract named “HelloWorld” with a single function called sayHello:

pragma solidity ^0.6.0;

contract HelloWorld {
    function sayHello() public pure returns (string memory) {
        return "Hello, World!";
    }
}

This contract has a single function, sayHello, which returns the string “Hello, World!”. The public keyword indicates that the function can be called from external contracts, and the pure keyword indicates that the function does not modify the state of the contract. The returns keyword specifies the return type of the function, which in this case is a string.

Data Types

Solidity has a variety of data types that you can use in your contracts. Here are some of the basic data types in Solidity:

  • bool: A boolean value (true or false)
  • int: An integer value
  • uint: An unsigned integer value
  • address: An Ethereum address
  • string: A string of characters

Here is an example of how to use these data types in a Solidity contract:

pragma solidity ^0.6.0;

contract DataTypes {
    bool myBool = true;
    int myInt = -42;
    uint myUint = 42;
    address myAddress = 0x1234567890;
    string memory myString = "Hello, World!";
}

In this contract, we’ve defined variables of each of the basic data types and assigned them initial values. Note that we’ve used the memory keyword for the string type to indicate that the string is stored in memory.

Arrays and Mappings in Solidity

In addition to basic data types, Solidity also supports arrays and mappings, which are types that allow you to store multiple values or key-value pairs, respectively.

Arrays

Arrays in Solidity are similar to arrays in other programming languages. They are a data type that allows you to store a collection of values of the same type.

Here is an example of how to use an array in a Solidity contract:

pragma solidity ^0.6.0;

contract Arrays {
    uint[] myArray;

    function setValue(uint index, uint value) public {
        myArray[index] = value;
    }

    function getValue(uint index) public view returns (uint) {
        return myArray[index];
    }
}

In this contract, we’ve defined an array named myArray of type uint. We’ve also defined two functions, setValue and getValue, which allow us to set and retrieve values in the array, respectively.

Mappings

Mappings in Solidity are a data type that allows you to store key-value pairs. The keys and values can be of any data type.

Here is an example of how to use a mapping in a Solidity contract:

pragma solidity ^0.6.0;

contract Mappings {
    mapping(string => uint) myMapping;

    function setValue(string memory key, uint value) public {
        myMapping[key] = value;
    }

    function getValue(string memory key) public view returns (uint) {
        return myMapping[key];
    }
}

In this contract, we’ve defined a mapping named myMapping that maps strings to unsigned integers. We’ve also defined two functions, setValue and getValue, which allow us to set and retrieve values in the mapping, respectively.

Conclusion

In this article, we’ve provided an overview of the basic syntax and data types in Solidity, including examples of how to use them in your contracts. We’ve also covered arrays and mappings, which are useful types for storing multiple values or key-value pairs. With these tools and techniques, you’re ready to start building your own smart contracts.

Exercises

To review these concepts, we will go through a series of exercises designed to test your understanding and apply what you have learned.

Write a Solidity contract that defines an array of integers and two functions, addValue and getValues, that allow you to add values to the array and retrieve all values in the array, respectively.

pragma solidity ^0.6.0;

contract ArrayExercise {
    int[] values;

    function addValue(int value) public {
        values.push(value);
    }

    function getValues() public view returns (int[] memory) {
        return values;
    }
}

Write a Solidity contract that defines a mapping of strings to integers and two functions, setValue and getValue, that allow you to set and retrieve values in the mapping, respectively.

pragma solidity ^0.6.0;

contract MappingExercise {
    mapping(string => int) values;

    function setValue(string memory key, int value) public {
        values[key] = value;
    }

    function getValue(string memory key) public view returns (int) {
        return values[key];
    }
}

Write a Solidity contract that defines a struct named Person with two fields, name and age, and a function named createPerson that creates a new instance of the Person struct and stores it in a mapping.

pragma solidity ^0.6.0;

contract StructExercise {
    struct Person {
        string name;
        uint age;
    }

    mapping(uint => Person) people;

    function createPerson(uint id, string memory name, uint age) public {
        Person memory person = Person({
            name: name,
            age: age
        });
        people[id] = person;
    }
}

Write a Solidity contract that defines an enumeration named Colors with three values, Red, Green, and Blue, and a function named getColor that takes an integer value as an input and returns the corresponding color from the Colors enumeration.

pragma solidity ^0.6.0;

contract EnumExercise {
    enum Colors { Red, Green, Blue }

    function getColor(uint colorId) public view returns (Colors) {
        return Colors(colorId);
    }
}

Write a Solidity contract that defines an array of integers and a function named findMax that returns the maximum value in the array.

pragma solidity ^0.6.0;

contract MaxExercise {
    int[] values;

    function findMax() public view returns (int) {
        if (values.length == 0) {
            return 0;
        }
        int max = values[0];
        for (uint i = 1; i < values.length; i++) {
            if (values[i] > max) {
                max = values[i];
            }
        }
        return max;
    }
}