What is Smart Contract in Solidity?
Last Updated :
22 Apr, 2023
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 Ethereum.
Now let's look at the code example for making smart contract :
Example #1: FirstContract
Solidity
pragma solidity ^0.5.0;
contract firstContract {
function helloGeek () public pure returns (string) {
return "HelloGeek !. This is your first Contract";
}
}
Code Explanation: In the above code, we've created a simple smart contract that has 1 method - helloGeek(); and its returning a string - "HelloGeek !. This is your first Contract" as output.
Now, let's look at another example:
Example #2: In this example, we'll be using set() function for setting the state variable value in the smart contract and get() function used for get this value from the smart contract.
Solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity >= 0.4.16 < 0.9.0;
/// @title A contract for demonstrate how to write a smart contract
/// @author Jitendra Kumar
/// @notice For now, this contract just show to set the value of state variable and get this value from the smart 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;
}
}
Explanation:
1. Version Pragma:
pragma solidity >=0.4.16 <0.9.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.9.0.
2. The contract keyword:
contract Storage{
//Functions and Data
}
The contract keyword declares a contract under which the code is 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.
Output:

Smart Contracts: Smart Contracts are the block of instructions that are not dependent on any third party or centralized database. They are executed in a decentralized environment.
The benefits of smart contracts are as follows:
- It removes centralized issues.
- It is safe and more secure.
- The terms and conditions are visible for the relevant parties. No way to change them.
- Makes the system more accurate.
Similar Reads
What is Wallet Smart Contract?
Wallets are just like your bank account, through which we can receive money, send money, and can check the balance. Important terms:uint- unsigned integer.address- It is a unique set of the string provided for the transaction.msg.sender- Address of the current user.msg.value- Cost put by the current
4 min read
What is Escrow Smart Contract?
Escrow is the third party that holds the asset(asset can be money, bond, or stocks) in the presence of two parties. Escrow will release the fund when certain conditions are met. For Example: "A" is a seller and wants to sell his car, and "B" is a buyer who wants to buy "A"'s car so they will contact
2 min read
Interaction Between Smart Contracts with Solidity
Interaction between two smart contracts in Solidity means invoking functions of one contract from another contract so that we can use the content of one contract in another. For example, if you want to use contractA methods into contractB, so simply import the contractA inside contract B and create
5 min read
What is a NFT Smart Contract?
NFT Smart contracts play an important role in the blockchain space across many use cases. In particular, NFT smart contracts are becoming increasingly important as the metaverse and Web3 attract more interest. NFT smart contracts are specialized blockchain contracts that facilitate the creation, own
8 min read
Decentralized Bank Smart Contract in Solidity
Solidity is an Object-oriented Programming Language for implementing Smart Contracts. It's a High-Level Statically Typed Language like C. The solidity file has an extension .sol. The article focuses on discussing the decentralized bank smart contract in Solidity. In the below example, the aim is to
3 min read
What is Solidity Compiler?
Solidity is a programming language that is used to write smart contracts on the Ethereum blockchain. Smart contracts are self-executing contracts that run on the blockchain and automatically enforce the terms of an agreement between parties. The Solidity compiler is an essential tool for developers
4 min read
Creating Ownable Contracts in Solidity
A Smart Contract (or crypto contract) is a computer program that directly and automatically controls the transfer of digital assets between the parties under certain conditions. Once deployed, a smart contract code cannot be changed. It is also publicly visible to everyone. Solidity is a language us
3 min read
Introduction to Solidity
Solidity is a brand-new programming language created by Ethereum which is the second-largest market of cryptocurrency by capitalization, released in the year 2015 and led by Christian Reitwiessner. Some key features of solidity are listed below: Solidity is a high-level programming language designed
5 min read
Solidity - Deploy a Smart Contract for Marks Management System
Solidity is a high-level language. The structure of smart contracts in solidity is very similar to the structure of classes in object-oriented languages. The solidity file has an extension .sol. What are Smart Contracts?Solidityâs code is encapsulated in contracts which means a contract in Solidity
3 min read
Type Conversion in Solidity
Solidity is a popular programming language specifically designed for developing smart contracts on the Ethereum blockchain. As a statically-typed language, Solidity offers various features to handle data types and conversions. Conversions play a crucial role in transforming data from one type to ano
6 min read