Solidity Variables

ยท

3 min read

Starting off with the very basics of solidity, this feels like having a superpower, Just like the very first time I wrote my C++ Hello world on my LG tab in 2016. That feeling got me so full of myself, Frequently talk to myself saying I am just like bill Gates, and Mark Zuckerberg. Well, I may be right. We all have something in common and that's the title of developer-only differences I don't have the experience in running a start-up, not a billion-dollar company. To me, this is all fun regardless of who I see as my role models.

Let's get into what I was able to sync out of the solidity main documentation today in my learning.

Solidity variables

Like every curious developer out there before learning any new technology, he wants to know what it's used for and how they're being structured.

What is solidity: Solidity is an object-oriented, high-level language for implementing smart contracts. Smart contracts are programmes that govern accounts' behaviour within the Ethereum state.

Solidity is basically used to write smart contracts that are Ethereum Virtual Machine EVM compatible. i.e There are other networks that are not EVM compatible.

An in-depth explanation of what smart contract means

Hello World Contract

This is the superpower I told you about, In solidity writing contracts is similar to declaring classes in other languages I am familiar with such as JavaScript.

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

contract  HelloWorld {
    //all other functionality in writing a dapp goes in here
}

The SPDX-License-Identifier: MIT is an open-source licence from MIT making use of the code, and in writing a solidity contract, we should declare the versioning for the compilers to understand what version we're building our dapp on.

Solidity is at its early stage and frequently gets updated. To specify the version of the Solidity we're using to build, we have two options which are either specifying the version directly while calling the pragma or giving it a range using ^, >, < and =.

On the next line, declare a class using the contract keyword and the name of the contract of choice. In this case, we're using Helloworld, which then every functionality goes into the class.

Variables

Solidity has variables declared into 3 parts namely;

  • Local variable

  • State variable

  • Global Variable

Local Variable: They're variables stored within a function call and therefore not stored on the blockchain i.e these kinds of variables can only be called within the scope of the function it was declared and are only called when the function is executing

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

contract  HelloWorld {
    //Local variables can only be called within the scope of their declared function

  function show() public {
      uint256 public name = "Izzy";
  }
}

State variable:* They're variables that are permanently stored on the blockchain. i.e they're stored in the storage of the contract.

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

contract  HelloWorld {
    //state variables are stored on the Blockchain.
    uint8 public name = "Izzy";
    address public owner = msg.sender;

  //function goes below here......
}

Global Variable: These are special variables existing in the global namespace to request specific information from the blockchain. They can also be within a function.

More on the global variable

pragma solidity ^0.8.0;

contract  HelloWorld {

    address sender = msg.sender;
    uint timestamp = block.timestamp;
  //function goes below here......
}

Enjoy the details of solidity language and how simplified it is to build Dapps.

LFG!!! ๐Ÿš€๐Ÿš€