![]() |
VOOZH | about |
Solidity is a smart contract programming language built solely to develop smart contracts that can be deployed on blockchains. C++ and JavaScript inspired it, an Object Oriented and High-Level language (HLL), that helps developers define the behaviour and rules for a Decentralized Application (Dapp). Smart contract facilitates autonomous, secure, and efficient transaction execution on the blockchain without needing any intermediary (self-executable) and solidity provides the base framework for achieving the same.
It is essential to understand the common keywords used in solidity to solidify your core understanding of the language so in this article, we'll pin down some prominent keywords that help us develop these smart contracts by embracing the true power of solidity language.
Table of Content
The keyword 'contract' as indicated by its name is used to define a smart contract.
The below code creates a contract named 'GeekContract', that has a single storage variable named : "sayHello()"
Constructor keyword is used to create a constructor which is a special type of function that will be executed as soon as the smart contract is Deployed to the chain, and is used to set the initial state variables in the contract and do some initilial startup work.
Note:
Constructor can only be declared using the 'constructor' keyword starting from solidity version ^0.6.0. And the old method to do so was using the Contract name which is DEPRECATED now.
The below contract code will have a public function for the state variable 'message' which will contain the string : "This is my first Contract!" and this string will be set on the deployment of this contract on chain.
Output:
The 'delete' keyword is used along with the state variables or structs for resetting their values. It helps in managing the contract state and resources by clearing the data structures values, resetting states etc.
Example:
Explanation:
The keyword 'enum' is used to declare an enumeration type consisting of only some of the selected pre-defined values that are called 'enumerators'.
Example:
Explanation:
A function is a set of code statements that are executed when a function is called from the inside (from another function) or outside of the smart contract.
1. The keyword function is used to declare a function followed by the function name, parameters it takes (wrapped in a parenthesis), visibility specifier like public, private, external or internal, type of function, return type (if any). Below is an example of function signature:
function functionName(parameter1 ..... parameterN) visibilitySpecifier modifier returns(datatype) () { // Code Logic Here }
2. Function Modifier: 'pure' is to prevent modification or state access, 'view' is for reading the state but preventing any modification, and 'payable' is so that functions can receive ethers when called. We'll learn more about these later in the article.
Explanation:
The above code is an extension of previous code, we've just added a new function named 'sayHello' which resets the value of message variable to "Hello Geek" when called.
Output:
The 'interface' keyword is used to declare an interface in solidity for providing a declaring for functions that will be implemented by smart contracts.
Explanation:
Above is an interface declared named 'MyToken' this interface can be implemented by any contract using inheritance's 'is' keyword.
In the context of Solidity, Inheritance refers to the property of once smart contract to acquire properties of another smart contract. The 'is' keyword is used for two purposes:
Explanation:
Output:
Import statements are nothing but extracting and accessing a piece of code that exists somewhere outside the current file. In solitidy, we can import from two places:
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "./MyOtherContract.sol";
Explanation:
This imports the File named "MyOtherContract".
'struct' keyword is used to declare a structure that consists of various different data types. It is used to declare a new 'type' in itself like a custom data type.
Explanation:
Visibility specifiers are analogous to the access specifiers or modifiers in object oriented languages, they define the visibility and accessibility of the function or variables.
Output:
Explanation:
As you can see, only the public and the external functions are visible in the output. Because private & internal functions are accessible on the contract, and the sub-contracts respectively.
The exception handling mechanism in solidity is defined for handling cases when the error is made on the user's end like invalid inputs, or even validate a condition.
Solidity and Blockchain is an ever evolving space, that's why many updates and breaking changes are made very often compared to any other technologies that's why it offers this feature to avoid compatibility issues.
Example:
pragma solidity ^0.8.25;
This defines that the smart contract will use solidity version >= (greater than equal to) 0.8.25.
It is used to specify the license version under which the code will be distributed (made available to everyone). Every license has their own set of protocols for right to use, code modification, copy writing guidelines etc. Solidity has some of the popular license under : SPDX (Software Package Data Exchange), Apache License, MIT License, GPL & LGPL etc.
Example:
Explanation:
The above code defines that the given source code will be licensed upon the GPL (General Public License) version 3.0 which gives the user the right to use, modify, study and distribute as well.
In Solidity, modifiers are special type of functions that helps us add some extra functionality to the existing functions. You can define a modifier by using keyword 'modifier' followed by its name, and parameters it may take.
Output:
Initially, before calling the customIncrement method:
After calling the customIncrement method:
In solidity, for effective memory management we can decide where to store the variables based on their nature or existence duration.
Explanation
Output:
Some variables are facilitated for accessing the global state of the blockchain :-
Fallback function is a special type of function in solidity that is called when either condition is met :-
Note:
This function is DEPRECATED starting from Cancun Hardfork, but we can still use it but it will NOT Destroy the underlying opcode in the blockchain but only transfer the ethers to the owner.
Explanation:
Output:
Both the keywords are typically used when we're trying to define assembly level bytecode for the EVM (Ethereum Virtual Machine) directly.
Example:
Explanation:
The function defines an assembly block where you can directly write assembly code.