Blockchain Solidity Basics
Solidity is the most popular language for writing smart contracts on Ethereum and similar networks. The syntax resembles JavaScript and C++, so many programmers pick it up quickly. A small first contract shows the main ideas.
A First Contract
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract SimpleStorage {
uint256 private favoriteNumber;
function store(uint256 newNumber) public {
favoriteNumber = newNumber;
}
function retrieve() public view returns (uint256) {
return favoriteNumber;
}
}
Line-by-Line Meaning
| Code | Meaning |
|---|---|
| SPDX license comment | Names the software license |
| pragma solidity | Sets the compiler version range |
| contract SimpleStorage | Starts a new contract, like a class |
| uint256 private favoriteNumber | Stores one whole number on the chain |
| function store | Changes the stored number |
| function retrieve | Reads the stored number |
The contract acts like a tiny locker. The store function puts a number in the locker. The retrieve function shows the number without changing it.
State Variables
State variables live permanently on the blockchain. Writing to them costs gas because every node must record the change. Local variables inside a function disappear after the function ends and cost far less.
Common Data Types
| Type | Holds | Example |
|---|---|---|
| uint256 | Positive whole numbers | 42 |
| int256 | Positive or negative numbers | -7 |
| bool | True or false | true |
| address | An account address | 0x followed by 40 characters |
| string | Text | "hello" |
| mapping | Key and value pairs | Balance for each address |
Function Visibility
public
Anyone can call a public function from inside or outside the contract.
private
Only the same contract can call a private function.
view
A view function reads data and changes nothing. Calling it from outside costs no gas.
Constructors and Owners
A constructor runs once when the contract goes live. Many contracts use the constructor to record the address of the creator as the owner. Later functions can check that address and allow only the owner to run sensitive actions. This simple pattern protects settings from strangers.
Reading Compiler Messages
The compiler checks your code before deployment. Error messages point to a line number and describe the problem. Missing semicolons, wrong types, and misspelled names cause most beginner errors. Read the message slowly, fix the first error, and compile again. Later errors often vanish after the first fix.
Events
Events write messages into the transaction log. Apps listen for events and update their screens. An event works like a doorbell that tells the outside world something just happened.
Deploy Process
Write code → Compile → Test on a test network → Deploy → Interact
A browser tool called Remix lets beginners write, compile, and deploy contracts without installing anything. Use a test network first. Test coins carry no real value, so mistakes cost nothing.
Safety Rules for Beginners
- Deployed contracts cannot be edited in a simple way, so test every function before deployment.
- Check every external call because attackers target unprotected functions.
- Use well-tested libraries for common features such as tokens.
- Hire a professional audit before any contract manages real funds.
Key Takeaways
Solidity contracts hold data and functions. State variables cost gas to change. Visibility words control who can call each function. Practice on a test network, and treat security as part of every line you write.
