A contract is like a digital agreement that runs on a blockchain. It tells the computer what to do and how to behave.
0
0
Contract structure and syntax in Blockchain / Solidity
Introduction
When you want to create a simple agreement that runs automatically, like a payment or a promise.
When you need to store and manage data securely on the blockchain.
When you want to build apps that work without a middleman, like games or voting systems.
When you want to make rules that everyone can trust and see on the blockchain.
Syntax
Blockchain / Solidity
contract ContractName {
// State variables
// Functions
}The contract starts with the keyword contract followed by its name.
Inside the curly braces { }, you write variables and functions that define the contract's behavior.
Examples
This contract has one variable called
number that stores a number.Blockchain / Solidity
contract SimpleContract {
uint number;
}This contract stores a message and has a function to change it.
Blockchain / Solidity
contract Greeting {
string public message;
function setMessage(string memory newMessage) public {
message = newMessage;
}
}Sample Program
This contract keeps a count number. It has a function to add one to the count and another to see the current count.
Blockchain / Solidity
pragma solidity ^0.8.0; contract Counter { uint public count; function increment() public { count += 1; } function getCount() public view returns (uint) { return count; } }
OutputSuccess
Important Notes
Contracts are like classes in other programming languages.
State variables store data on the blockchain and keep it safe.
Functions define what the contract can do and can be called by users.
Summary
A contract is a digital agreement that runs on the blockchain.
It starts with the contract keyword and has variables and functions inside.
Contracts help build secure and automatic programs on the blockchain.