How to Write Smart Contracts in Solidity: Simple Guide
To write a smart contract in
Solidity, start by defining a contract with state variables and functions inside. Use pragma solidity to specify the compiler version and write your logic in functions that can read or change the contract's data.Syntax
A Solidity smart contract starts with a pragma statement to set the compiler version. Then, use the contract keyword to define the contract. Inside, declare state variables to store data and functions to perform actions. Functions can be public, private, or external to control access.
Use uint for numbers, string for text, and address for Ethereum addresses. Functions can modify state or just read data using view or pure keywords.
solidity
pragma solidity ^0.8.0; contract MyContract { uint public myNumber; // state variable // function to set the number function setNumber(uint _num) public { myNumber = _num; } // function to get the number function getNumber() public view returns (uint) { return myNumber; } }
Example
This example shows a simple contract that stores and retrieves a number. It demonstrates how to declare a state variable, write functions to change and read it, and use visibility keywords.
solidity
pragma solidity ^0.8.0; contract SimpleStorage { uint private storedNumber; // Store a new number function store(uint _number) public { storedNumber = _number; } // Retrieve the stored number function retrieve() public view returns (uint) { return storedNumber; } }
Output
No direct output; functions store and retrieve data on the blockchain.
Common Pitfalls
- Not specifying compiler version: Always use
pragma solidityto avoid compatibility issues. - Incorrect visibility: Forgetting to set function visibility defaults to
internal, making functions inaccessible externally. - State changes in view functions: Functions marked
vieworpurecannot modify state. - Uninitialized variables: Variables have default values; forgetting to initialize can cause bugs.
solidity
/* Wrong: Missing visibility (defaults to internal) */ function set(uint _num) internal { storedNumber = _num; } /* Right: Explicit public visibility */ function set(uint _num) public { storedNumber = _num; }
Quick Reference
| Concept | Description | Example |
|---|---|---|
| pragma | Sets compiler version | pragma solidity ^0.8.0; |
| contract | Defines a smart contract | contract MyContract { ... } |
| state variable | Stores data on blockchain | uint public myNumber; |
| function | Performs actions | function setNumber(uint _num) public { ... } |
| visibility | Controls access | public, private, external, internal |
| view/pure | Read-only functions | function get() public view returns(uint) { ... } |
Key Takeaways
Always start with a pragma statement to specify Solidity version.
Define contracts with state variables and functions to store and manage data.
Set explicit function visibility to control who can call them.
Use view or pure for functions that only read data without changing state.
Test your contract carefully to avoid common mistakes like uninitialized variables.