Solidity basics part 2:

·

3 min read

Memory and storage

In solidity, the memory keyword is a temporary place to store information vs the storage keyword which holds data between function calls. We can use as much memory as we want but whenever the function is executed the memory is completely gone for the next set of executions, this essentially costs nothing in terms of gas which is why we use it. Meanwhile, storage is more permanent, meaning each new execution has access to previous executions. It's advisable to use the memory keyword when you're checking/writing code that is not going to be placed on a public blockchain. When all the checks etc are completed, use the storage keyword.

Return statement

In solidity, we are able to use the return keywords to return values from functions. This should usually be the last statement in a function. In the example below I will show you the syntax for incorporating return statements into your functions.

string Friends = "Hey guys"

function heyGuys() public returns(string memory){
    return Friends;
}

In this example, the string will return the string by adding the return statement.

View and Pure

In solidity, if the behavior of a function is not specified it will read and modify the state of the blockchain. View functions allow us to read-only but it does not allow us to change any of the code while with pure functions you cant view or change any of the code. Pure functions essentially ensure that contracts outside cannot read or modify the state of your contract. Another use of pure and view functions is that due to the fact that you cant modify the state of the contract they consume zero gas. If you run these functions with something like web3.js it will run off of your own node while if you executed the same function normally it would require a miner to execute this function this consuming gas, the less gas one can consume the better the code. Below I will show you exactly how to write these types of functions.

function heyGuys() public view returns(string memory){

}



function _add(uint a,  uint b) private pure returns(uint){

   return a + b;
}

Keccak256

Keccak256 is a cryptography function built into the solidity programming language. Its function is to take in any amount of input and convert them into a unique 32-byte hash. Keccak256 belongs to the keccak family which is a family of cryptographic hash functions. Hashes are important when working with solidity and the blockchain because you can use them to do stuff like authenticate payloads of data. keccack256 expects a single parameter of type bytes this means that we have to pack any parameter before calling keccak256. I will show you what I mean in the example below.

//6e91ec6b618bb462a4a6ee5aa2cb0e9cf30f7a052bb467b0ba58b8748c00d2e5
keccak256(abi.encodePacked("aaaab"));
//b1f078126895a1424524de5321b339ab00408010b7cf0e6ed451514981e58aa9
keccak256(abi.encodePacked("aaaac"));

As you can see in this example the string is completely different despite the fact that only one letter has been changed. We can use this to generate random numbers.

Typecasting

Typecasting is essentially converting from one data type to another. It is a pretty easy concept to understand, we essentially use it to ensure that a function handles the variables correctly. Below I will show you exactly we use typecasting in solidity.

uint p = 12
uint8 q  = 24

// This is gonna throw an error because p and q returns a uint not a uint8
uint8 r = p * q;

// The solution for a problem like this would be to typecast q to make it a uint8
uint8 r = p * uint8(q);

Conclusion

I have explained the importance of memory, storage, return statements, view, pure, keccak256, and typecasting. In the next section of this series, I will take a deeper dive into solidity and explain more complex topics that I feel like will every beginner to solidity should know.