0
0
Blockchain / Solidityprogramming~10 mins

Contract structure and syntax in Blockchain / Solidity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Contract structure and syntax
Start Contract
Declare Contract Name
Define State Variables
Write Functions
Add Constructor (optional)
Close Contract
This flow shows the basic steps to write a contract: start, name it, add variables, write functions, optionally add a constructor, then close.
Execution Sample
Blockchain / Solidity
contract Simple {
  uint count;
  function increment() public {
    count += 1;
  }
}
A simple contract named Simple with a variable count and a function to increase it by 1.
Execution Table
StepActionCode LineEffectState Change
1Start contract declarationcontract Simple {Contract named Simple createdNo state variables yet
2Declare state variableuint count;Variable count of type uint declaredcount = 0 (default)
3Define function incrementfunction increment() public {Function increment createdNo state change
4Increment countcount += 1;count increased by 1count = 1
5Close function}Function increment endscount = 1
6Close contract}Contract Simple endsFinal state: count = 1
💡 Contract code ends after closing brace, state variable count is 1 after increment function call
Variable Tracker
VariableStartAfter Step 4Final
count0 (default)11
Key Moments - 3 Insights
Why do we declare variables outside functions?
Variables declared outside functions are state variables stored on the blockchain, as shown in step 2 where count is declared before functions.
What does 'public' mean in the function declaration?
'public' means the function can be called from outside the contract, allowing interaction, as seen in step 3.
Why is the contract closed with a '}'?
The closing brace '}' marks the end of the contract code block, as in step 6, signaling no more code inside the contract.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of count after step 2?
Aundefined
B1
C0
DError
💡 Hint
Check the 'State Change' column at step 2 in the execution table.
At which step does the count variable increase?
AStep 4
BStep 2
CStep 3
DStep 6
💡 Hint
Look for the action 'Increment count' in the execution table.
If we remove the closing brace at step 6, what happens?
AContract compiles successfully
BSyntax error, contract not closed
CFunction increment won't work
Dcount variable resets
💡 Hint
The exit note says the contract ends after the closing brace; missing it causes syntax errors.
Concept Snapshot
Contract structure:
- Start with 'contract Name {'.
- Declare state variables outside functions.
- Write functions inside contract.
- Use visibility keywords like 'public'.
- Close contract with '}'.
- State variables hold data on blockchain.
Full Transcript
This visual trace shows how a simple blockchain contract is structured and how its syntax works. We start by declaring the contract with a name. Then we declare state variables outside functions to store data on the blockchain. Next, we write functions inside the contract to manipulate these variables. Functions can have visibility like 'public' to allow external calls. The contract code ends with a closing brace. The execution table traces each step, showing how the variable 'count' starts at zero and increases by one when the increment function runs. Key moments clarify common confusions about variable placement, function visibility, and code block closure. The quiz tests understanding of variable values at steps, when changes happen, and syntax rules. This helps beginners see how contract structure and syntax work together to create blockchain programs.