0
0
BlockchainHow-ToBeginner ยท 4 min read

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 solidity to avoid compatibility issues.
  • Incorrect visibility: Forgetting to set function visibility defaults to internal, making functions inaccessible externally.
  • State changes in view functions: Functions marked view or pure cannot 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

ConceptDescriptionExample
pragmaSets compiler versionpragma solidity ^0.8.0;
contractDefines a smart contractcontract MyContract { ... }
state variableStores data on blockchainuint public myNumber;
functionPerforms actionsfunction setNumber(uint _num) public { ... }
visibilityControls accesspublic, private, external, internal
view/pureRead-only functionsfunction 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.