What is Smart Contract in Solidity?

Solidity’s code is encapsulated in contracts which means a contract in Solidity is a collection of code (its functions) and data (its state) that resides at a specific address on the Ethereum blockchain. A contract is a fundamental block of building an application on the Ethereum. 







// Solidity program to 
// demonstrate how to
// write a smart contract 
pragma solidity >= 0.4.16 < 0.7.0;
// Defining a contract
contract Storage
    // Declaring state variables
    uint public setData;
    // Defining public function 
    // that sets the value of 
    // the state variable
    function set(uint x) public
        setData = x;
    // Defining function to
    // print the value of
    // state variable
    function get(
    ) public view returns (uint) {
        return setData;



1. Version Pragma: 

pragma solidity >=0.4.16 <0.7.0;

Pragmas are instructions to the compiler on how to treat the code. All solidity source code should start with a “version pragma” which is a declaration of the version of the solidity compiler this code should use. This helps the code from being incompatible with the future versions of the compiler which may bring changes. The above-mentioned code states that it is compatible with compilers of version greater than and equal to 0.4.16 but less than version 0.7.0.

2. The contract keyword: 

contract Storage{ 
//Functions and Data 

The contract keyword declares a contract under which is the code encapsulated.

3. State variables: 

uint public setData;

State variables are permanently stored in contract storage that is they are written in Ethereum Blockchain. The line uint setData declares a state variable called setData of type uint (unsigned integer of 256 bits). Think of it as adding a slot in a database.

4. A function declaration:

function set(uint x) public
function get() public view returns (uint)
  • This is a function named set of access modifier type public which takes a variable x of datatype uint as a parameter. 
    This was an example of a simple smart contract which updates the value of setData. Anyone can call the function set and overwrite the value of setData which is stored in Ethereum blockchain and there is possibly no way for anyone to stop someone from using this function. This is an example of a decentralized application that is censorship proof and unaffected to the shutdown of any centralized server. As long as someone is running a single node of Ethereum blockchain, this smart contract will be accessible.
  • Function get will retrieve and print the value of the state variable.


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.