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

Variable: Difference between revisions

From Groundhog Learning
Updated via bot
 
m Updated via bot
 
Line 1: Line 1:
<span id="variablevariable"></span>
<span id="variablevariable"></span>


A named place in a [[Computer_Program|computer program]] where you can store and change a value.
A named container in a [[Computer_Program|computer program]] where you can store, retrieve and change a value.


<span id="details"></span>
<span id="details"></span>
== Details ==
== Details ==


Variables are like labeled boxes: you can store something inside, check what’s there, and update it.
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.
 
They make programs flexible by allowing handling values that change during execution.


Consider keeping track of a player’s score:
Consider keeping track of a player’s score:
Line 16: Line 14:
<syntaxhighlight lang="console">score = 0                   # store the initial value
<syntaxhighlight lang="console">score = 0                   # store the initial value
score = 10                  # change the value
score = 10                  # change the value
"Current Score:" score      # use the current value
"Current Score:" score      # read the current value
</syntaxhighlight>
</syntaxhighlight>
A variable creates a space in memory called <code>score</code> to hold the player’s score.
A variable creates a space in memory called <code>score</code> to hold the player’s score value. This allows the game to update the score as it changes and display it to the player.
 
 
-----


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 [[Variable_Declaration|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.




-----
-----


To use a variable, we need to declare it first.
[[Naming_Variables|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.


Declaring a variable, consists of telling the program to reserve a piece of memory to store some data.
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|Related Actions]] for details on how to perform different actions with variables.
Check [[#related-actions|Related Actions]] for details on how to perform different actions with variables.
Line 35: Line 39:


* ''Has'': [[Data_Type|Data Type]]
* ''Has'': [[Data_Type|Data Type]]
* ''Used in'': [[Variable_Declaration|Variable Declaration]]
* ''Used in'': [[Naming_Variables|Naming Variables]]
* ''Used in'': [[Naming_Variables|Naming Variables]]



Latest revision as of 17:25, 23 October 2025

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