Solidity 2

TDhendup
0

 

Solidity 2-Functions and Data Types in Solidity

Functions in solidity are ‘blocks of code that perform specific tasks or operations within a smart contract’. To understand the functions in solidity, let’s do the following example. We declare function with the keyword ‘function’ followed by the ‘function_name’. We will be using the getter() and the setter() functions in the following example.

In the above diagram, (line 15) we pass the parameters through setting the variable unit_numValue and to make the function available to the outside world, we set as ‘public’. (line 16), we set the value _numValue to the variable ‘num’. (line19), we define another function getter() to get the value of ‘num’ and the function to be ‘public’. The word ‘view’ is used to read the state variable, and the word ‘returns’ followed by the datatype. (line 20), to return the value we use ‘return’. During the deployment, we provide value by entering the value and clicking on the ‘setter’ button, followed by the click on the ‘getter’ button to get the value entered.

However, when we create a public state variable, we do not need to use the getter() function. This is because, the getter function is automatically created when we define as ‘public’’ for the state variables. (line 19) even if we do not have the getter() function, we see the button ‘num’ is created. Clicking on ‘num’ button will return ‘zero’ as in Solidity, we do not have the concept of ‘null’ or ‘none’ like other languages as when we declare variables without initializing, then the value set will be done according to the ‘datatype’. Since the datatype is uint, we get the value ‘zero’.

Let’s perform some calculations as shown above by incrementing the ‘num’ variable by one. Initially, the ‘num’ value will be ‘zero’. Let’s call the setter function every time we click on ‘setter’ button. For the value ‘5’ above, we have clicked on ‘setter’ button 5 times to get the value of num as ‘5’.

Let’s talk about the word ‘pure’. In this case, we have comment the setter() function and uncomment the getter() function. We use ‘view’ and ‘pure’ keywords when we do not modify the ‘state variables’. But ‘pure’ can’t be used when the state variables are being ‘read’ i.e., ‘pure’ is used when we do not read or write to the state variables. In case of ‘view’, the read is allowed but ‘write’ is not allowed. With the word ‘pure’, we will get the error during the deployment of the program.

Therefore, we define uint ‘a’ and assign to 50; and let me return it. With this we will not get the ‘error’ as we are not writing or reading to the state variable.

In the above 2 figures, we will be demonstrating the unsigned and signed integers. The unsigned integer can store either ‘zero’ or ‘positive’ values so it starts with uint. Uint is the abbreviation for uint256 as this is the largest possible size, while you can also define as uint8, uint16 depending on the size of the data.

In the first figure, (line 4), we assign variable ‘a’ to 50. (line 5), we declare signed integers. (line 6), we define the Boolean by ‘bool’ keyword and can have the value of ‘true’ or ‘false’. (line 7) If we do not initialize the value in ‘boo’, we will get the value ‘false’ by default. (line 8), we will store the Ethereum address/Wallet address (the one we created during the metamask installation time) and the keyword ‘address’ is the unique datatype specific to solidity.  (line 9) will create the textual data and the keyword used is ‘string’.

Let’s look at the arrays. There are two types of arrays viz., ‘fixed’ and ‘dynamic’ arrays. The demonstration on ‘fixed’ and ‘dynamic’ arrays can be seen in the above figures. While calling the values from the arrays, we will call by the ‘index’ such as ‘0’ for the value ‘10’ and ‘1’ for the value ‘20’.

Practise

1. Create a new solidity file on Remix IDE

2. Use the uint data type to define an unsigned integer - counter

3. Create a function incrementCounter()

4. User the public keyword to define the visibility of the counter variable and the incrementCounter() function

5. incrementCounter() function to increase the value of the 'counter' variable by 1 each time it is called.

 

pragma solidity >=0.8.2 <0.9.0;

contract MyContract {

    uint public counter;

function incrementCounter() public {

    counter = counter + 1;

}

}

Post a Comment

0Comments

Will review your comment and get back!

Post a Comment (0)