Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Variable

From Groundhog Learning
Revision as of 17:25, 23 October 2025 by Groundhog (talk | contribs) (Updated via bot)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

A named container in a computer program where you can store, retrieve and change a value.

Details

Variables are like labeled boxes: you can store something inside, check what’s there, and update it. They make programs flexible by allowing you to work with data that isn’t known ahead of time.

Consider keeping track of a player’s score:

🤖 Pseudocode

score = 0                   # store the initial value
score = 10                  # change the value
"Current Score:" score      # read the current value

A variable creates a space in memory called score to hold the player’s score value. This allows the game to update the score as it changes and display it to the player.



Before you can use a variable, you must first declare a variable in your program. You can also initialize it with a starting value, and once declared, you can reference it anywhere in your code to read or update its value.



Naming variables is surprisingly hard — and very important. A variable name is more than just a label; it communicates the purpose of the value stored in memory.

Poorly chosen names can make code confusing, difficult to read, and hard to maintain, especially in larger programs. Good variable names help both you and others understand what the code is doing at a glance.



Check Related Actions for details on how to perform different actions with variables.

Related Concepts

Related Actions

References