State Variables
Part of the Fundamentals section of Coddy's Solidity journey. Lesson 26 of 53.
A variable declared inside the contract but outside every function is a state variable. Functions in that contract share it. A local variable declared inside a function belongs to that call. On a deployed blockchain, successful state changes can persist between transactions.
Declare this at contract scope:
uint256 count;Use it inside the provided main function:
count = 6;
console.log(count);count is declared at contract scope and main assigns it. This practice runner starts with fresh zeroed storage for every test, so do not expect one test to continue another. For this section, initialize state through assignments inside main rather than declaration initializers or constructors.
A state variable is declared at contract scope, outside function bodies.
Challenge
EasyDeclare a state variable visits. In main, set it to the supplied start, call an internal helper addVisit that increases visits by one, then print visits.
The tests pass arguments to main in this order: uint256 start. Each test starts with fresh contract state.
Use console.log for the requested output. Print only the requested values, one per line, with no extra labels unless the task specifies them.
Try it yourself
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;
import "forge-std/console.sol";
contract Main {
function main(uint256 start) external {
// Write your solution here.
}
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
Practice on your own: Solidity playground