Monday 17 September 2018

Solidity - II

After the first post on Solidity in Remix, we look at the basic types that are encountered while programming in Solidity. We will be using the online Remix IDE for all the work in this post.

Let us see the program below that shows us the default values of the basic data types:

pragma solidity ^0.4.25;

contract Types {
        bool Boolean;
        int SignedInteger;
        uint UnsignedInteger;
        address Address;
        byte Byte;
        string String;
        function getDefaultValues() public view returns (bool, int, uint, address, byte, string) {
    return (Boolean, SignedInteger, UnsignedInteger, Address, Byte, String);
        }
}

The function defaultValues() is a getter function. view is used bool is be used to define boolean variables in Solidity. int stands for signed integers of 256 bits. We can define int of 8 bits as int8 and then multiples of 8 like int16, int24 and so on till int256. uint represents unsigned integers and is of 256 bits. unit can also be represented in the same way as int: uint8, unit16, uint24, ..., unit256. address type defines an Ethereum address and is of 20 bytes. byte is byte array and is equal to bytes1. Like int, it also variants like bytes2, bytes3, and so on till bytes32. string type defines strings. Compiling the above contract in Remix gives the below result:













Running and deploying we see below result:













Note that there is no output for string data type. Now let us do a few operations on the default values:

pragma solidity ^0.4.25;

contract Types {
        bool Boolean;
        int SignedInteger;
        uint UnsignedInteger;
        address Address;
        byte Byte;
        string String;
        function changedValues() public view returns (bool,int,uint,address,byte,string) {
    return (!Boolean, SignedInteger-1, UnsignedInteger+2, Address, Byte|0x10, String);
        }
}

Running above program shows below result:


We can compare the results with the results of the earlier run and see that values of 4 variables have changed. In the last example, we show the classic overflow and underflow examples in int8:

pragma solidity ^0.4.25;

contract IntTypes {
        int8 i=1;
        int8 j=2;
        int8 k=127;
        int8 l=-128;
        int8 m=3;
        function setInt() public {
            i++;
            j--;
            k++;
            l--;
            return;
        }
        function getInt() public view returns (int8,int8,int8,int8,int8) {
        return (i, j, k, l, m++);
        }
}

Let us run the above program. When we try to deploy the program, we get two buttons called setInt and GetInt in line with the function names as shown below:











Now, click on getInt button to see default values as shown below:













Now that i, j, k, and l are in line with the values that we have set when we defined these variables. But, the ++ increment operator fails on m as it does not increment m and returns the same value that is set when it is defined. Now, click on setInt button and then getInt again to see the results below:












Note that i is incremented from 1 to 2, j is decremented from 2 to 1, k overflows and is reset to -128, l underflows and is reset to 127. The overflow and underflow occur as the range of values that int8 can take are from -128 to 127. Note again that m remains the same and is not incremented. While ++ increment operator or -- decrement operator work well within the body of the function, they do not operate on the variable in the return clause.

This concludes the second post on Solidity