A contract is like a digital agreement that runs on a blockchain. It tells the computer what to do and how to behave.
Contract structure and syntax in 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.
number that stores a number.contract SimpleContract {
uint number;
}contract Greeting {
string public message;
function setMessage(string memory newMessage) public {
message = newMessage;
}
}This contract keeps a count number. It has a function to add one to the count and another to see the current count.
pragma solidity ^0.8.0; contract Counter { uint public count; function increment() public { count += 1; } function getCount() public view returns (uint) { return count; } }
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.
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.
Practice
What is the correct way to start defining a contract named SimpleStorage in Solidity?
Solution
Step 1: Understand Solidity contract declaration
In Solidity, contracts are declared using the keywordcontractfollowed by the contract name and curly braces.Step 2: Match the syntax with the options
Only contract SimpleStorage { } usescontract SimpleStorage { }, which is the correct syntax to start a contract.Final Answer:
contract SimpleStorage { } -> Option AQuick Check:
Contract declaration = contract SimpleStorage { } [OK]
- Using 'class' instead of 'contract'
- Using 'function' to define contract
- Using 'module' which is not valid in Solidity
Which of the following is the correct syntax to declare a state variable count of type uint inside a contract?
Solution
Step 1: Recall Solidity variable declaration syntax
State variables are declared by specifying the type followed by the variable name and a semicolon.Step 2: Check each option
uint count; correctly declaresuint count;. uint count = 0 misses the semicolon. Options B and D use incorrect syntax.Final Answer:
uint count; -> Option CQuick Check:
Variable declaration = type name; [OK]
- Omitting semicolon at the end
- Using 'var' keyword which is not valid in Solidity
- Reversing type and variable name
What will be the output of the following Solidity function when called?
contract Test {
uint number = 5;
function getNumber() public view returns (uint) {
return number + 3;
}
}Solution
Step 1: Understand the variable and function
The state variablenumberis set to 5. The function returnsnumber + 3.Step 2: Calculate the return value
5 + 3 equals 8, so the function returns 8.Final Answer:
8 -> Option BQuick Check:
5 + 3 = 8 [OK]
- Confusing variable value with function output
- Assuming function returns variable directly without addition
- Thinking there is a syntax error
Identify the error in the following Solidity contract snippet:
contract Counter {
uint count
function increment() public {
count += 1;
}
}Solution
Step 1: Check variable declaration syntax
The lineuint countis missing a semicolon at the end, which is required in Solidity.Step 2: Verify other parts
Function visibility aspublicis valid. Variables are correctly declared outside functions. Contract names can be uppercase.Final Answer:
Missing semicolon after uint count -> Option DQuick Check:
Semicolon missing = syntax error [OK]
- Forgetting semicolon after variable declaration
- Thinking function visibility must be private
- Assuming contract names must be lowercase
You want to create a contract that stores a person's name and age, and has a function to update the age. Which of the following contract structures correctly implements this?
contract Person {
string name;
uint age;
function setAge(uint newAge) public {
age = newAge;
}
}Solution
Step 1: Check variable declarations
Variablesnameandageare declared at contract level, which is correct for storing data.Step 2: Check function to update age
The functionsetAgetakes a parameter and assigns it toage, correctly updating the state variable.Final Answer:
Correct: variables declared and function updates age properly -> Option AQuick Check:
State variables + update function = correct contract [OK]
- Declaring variables inside functions instead of contract
- Expecting update functions to return values unnecessarily
- Forgetting to declare variables at contract level
Ready to prove it?
Pass the test and this pattern moves unknown
