In this article, I will provide some Basic Programs in Solidity.
- Hello World Program.
pragma solidity ^0.8.0;
contract HelloWorld {
string greeting = "Hello, World!";
function sayHello() public view returns (string memory) {
return greeting;
}
}
- Simple Math Operations.
pragma solidity ^0.8.0;
contract SimpleMath {
uint256 public sum;
uint256 public product;
function add(uint256 a, uint256 b) public {
sum = a + b;
}
function multiply(uint256 a, uint256 b) public {
product = a * b;
}
}
- Simple Storage.
pragma solidity ^0.8.0;
contract SimpleStorage {
uint256 storedData;
function set(uint256 x) public {
storedData = x;
}
function get() public view returns (uint256) {
return storedData;
}
}
- Structs and Arrays.
pragma solidity ^0.8.0;
contract StructsAndArrays {
struct Person {
string name;
uint256 age;
}
Person[] public people;
function addPerson(string memory _name, uint256 _age) public {
people.push(Person(_name, _age));
}
}
- Mapping.
pragma solidity ^0.8.0;
contract MappingExample {
mapping (uint256 => string) public names;
function addName(uint256 id, string memory name) public {
names[id] = name;
}
}
- Function Modifiers.
pragma solidity ^0.8.0;
contract FunctionModifiers {
address public owner;
constructor() {
owner = msg.sender;
}
modifier onlyOwner() {
require(msg.sender == owner, "Only owner can call this function.");
_;
}
function doSomething() public onlyOwner {
// function code goes here
}
}
- Inheritance.
pragma solidity ^0.8.0;
contract ParentContract {
uint256 public x;
function setX(uint256 _x) public {
x = _x;
}
}
contract ChildContract is ParentContract {
function doSomething() public {
// function code goes here
}
}
- Events.
pragma solidity ^0.8.0;
contract EventExample {
event NewPerson(string name, uint256 age);
function addPerson(string memory _name, uint256 _age) public {
emit NewPerson(_name, _age);
}
}
- Payable Functions.
pragma solidity ^0.8.0;
contract PayableFunction {
function buySomething() public payable {
// function code goes here
}
}
- Time-Based Functions.
pragma solidity ^0.8.0;
contract TimeBasedFunction {
uint256 public startTime;
function start() public {
startTime = block.timestamp;
}
function elapsedTime() public view returns (uint256) {
return block.timestamp - startTime;
}
}
- Random Number Generation.
pragma solidity ^0.8.0;
contract RandomNumber {
function getRandomNumber(uint256 max) public view returns (uint256) {
uint256 randomNumber = uint256(keccak256(abi.encodePacked(block.timestamp, msg.sender))) % max;
return randomNumber;
}
}
- Enums.
pragma solidity ^0.8.0;
contract EnumsExample {
enum State { PENDING, APPROVED, REJECTED }
State public state;
function setState(uint
Further Reading
Some Examples of MongoDB Documents
20 Project Ideas on Finance in PHP for College Students
20+ Interview Questions on Solidity
Creating Single Page Applications with Angular
Angular 10 Data Binding in Different Ways
Creating Some Angular Components
Examples of Array Functions in PHP
- AI
- Android
- Angular
- ASP.NET
- Augmented Reality
- AWS
- Bioinformatics
- Biometrics
- Blockchain
- Bootstrap
- C
- C#
- C++
- Cloud Computing
- Competitions
- Courses
- CSS
- Cyber Security
- Data Science
- Data Structures and Algorithms
- Data Visualization
- Datafication
- Deep Learning
- DevOps
- Digital Forensic
- Digital Trust
- Digital Twins
- Django
- Docker
- Dot Net Framework
- Drones
- Elasticsearch
- ES6
- Extended Reality
- Flutter and Dart
- Full Stack Development
- Git
- Go
- HTML
- Image Processing
- IoT
- IT
- Java
- JavaScript
- Kotlin
- Latex
- Machine Learning
- MEAN Stack
- MERN Stack
- Microservices
- MongoDB
- NodeJS
- PHP
- Power Bi
- Projects
- Python
- Quantum Computing
- React
- Robotics
- Rust
- Scratch 3.0
- Shell Script
- Smart City
- Software
- Solidity
- SQL
- SQLite
- Tecgnology
- Tkinter
- TypeScript
- VB.NET
- Virtual Reality
- Web Designing
- WebAssembly
- XML