Digesting the Solidity

Krishna Kant
Coinmonks
4 min readJul 24, 2022

--

Remember where we had taken a cess in the last article?
contract pay{
function payEther() public payable{
}}

The above snippet is the code for sending some crypto payment to a particular address but it seems to be an alien language. No? To understand this well, let us take one dive more to understand everything about Solidity.
But before that, I think we can describe and understand the basic code written below with whatever chunks of information we have gained till now.

contract Identity{
string name;
unit age;
constructor() public{
name=’Ravi’;
age = 17;
}
function getName() view public returns(string memory){
return name;
}
function getAge() view public returns(uint){
return age;
}

function setAge() public{
age=age+1;
}
}

here in the above code snippet, we have tried to write a program in solidity where we have written an Identity object with key-value pairs as name and age. We are going further to get the name and age from the above-declared interface and increase the age value by 1 whenever we want to set it.

Getter and Setter

In the above snippet, we came across getter and setter terms. These are nothing but the terminology to express the methods. Getter is the term used to say that we are trying to get something out of the program, while setter is used to indicate that we are going to edit any prior variable with some new data and exchange its existence with a new set of changes.
For example, here-

construct local{
unit age = 10;

function getter() public view returns(unit){
return age;
}

function setter() public{
age=age+1;
}

////or////

function setter(unit newAge) public{
age=newAge;
}

}
Hold on guys… what just happened here? Honestly speaking, Solidity is our host, hence solidity has got to quench our thirst for curiosity here. Let's not judge us, let's just trust Solidity.
So, here in the above code, at first, we have declared the age variable with its value as 10. Later, with the ‘getter’ function, we are getting the value of age.
Next, with the ‘setter’ function, we are setting the value of the age variable firstly with a fixed value of 1 and then with an arbitrary value of what the user enters.
Now you must be thinking that why I have used (or)? That is because we cannot use the same name of a function while defining more than one. Of course, we can use some other name for the second getter function like getter_two or abra_ka_dabra.
Also, one thing to notice here is that while creating the interface, we have written unit age = 10; It must be understood that instead of this if we write unit public age =10; then we do not even need to make any getter function or as such to get the value of age. It is so because we already initialised the keyword ‘public’ and that will show the value of variable age by default.
Now let us see some traits of getter and setter functions-

  • Calling a setter function creates a transaction that needs to be mined and costs gas because it changes the blockchain whereas there is nothing as such with a getter function,
  • When we declare the public state variable, a getter function is automatically created,
  • By default, variable visibility is private.

view vs. pure

Till now in most of the programming functions, we have seen ‘view’ and ‘pure’ like words while declaring any function.
Let us know more about them-

  • ‘view’ and ‘pure’ are used where we are not doing any updates/writing,
  • ‘pure’ is used where reading from state variables is not required,
  • ‘view’ is used where reading from the state variable is needed.

Storge vs. Memory

The next most important concept we can come across is the storage and memory aspects of Solidity.
Storage

  • Holds state variable,
  • It is persistent across the program,
  • It costs gas fees,
  • It is more or less like a computer HDD

Memory

  • Holds local variables inside functions if they are reference types,
  • Not persistent,
  • No gas fees are required,
  • It is like a computer RAM

Global variables in Solidity

These are the built-in variables used for vpurposespurpose like knowing the time-stamp, block creators etc.
An example of using the global variables-
construct demo{
function get() public view returns(unit block_no. uint timestamp, address, msgSender){
return (block. number, block. timestamp, msg. sender);
}
}

Now, having gone through the variety of Solidity code snippets, let us go back to where we had started-
our payable function —-(to be continued). The villain is yet to come into our trap. I will make it clear for the understanding in our next article, till then let's keep chaining the blocks.

Thanks for your time guys!

New to trading? Try crypto trading bots or copy trading

--

--