Solidity Basics: Your First Lines of Code — Key concepts
Know how a Solidity file is structured.
Licensing and versionsSPDX identifiers state license; pragma fixes compiler range. Teams lock pragmas to avoid accidental behavior changes across compiler versions.
Contract anatomyA contract bundles state variables, functions, events, errors, and modifiers. Visibility (public, external, internal, private) defines the ABI surface; state mutability (view, pure, payable) signals intent.
Data locationsStorage persists on-chain; memory is temporary; calldata is read-only function input. Choosing correctly affects gas and correctness.
Getters and ABIPublic state variables auto-generate getters and expand the ABI. Public/external functions become callable from outside; internal/private stay inside inheritance boundaries.
Function selectorsEvery external call is routed by a 4-byte selector derived from the function signature.
function square(uint256 x) public pure returns (uint256) {
return x * x;
}
function updateGreeting(string calldata text) external {
greeting = text; // storage write, costs gas
}
Changing parameter types changes the selector. Knowing this helps you read raw calldata, debug mismatches, and avoid overloaded functions that confuse integrators.
- SPDX + pragma:Declare license and lock compiler range.
- Visibility:Public/external/internal/private for functions and state.
- Mutability:View, pure, payable make intent explicit.
- Data location:Storage (persistent), memory (temp copy), calldata (read-only args).
- Events:Cheap logs for off-chain consumers; not a storage substitute.
- Errors/modifiers:Reusable guards; prefer custom errors for gas.