# Variable
A symbolic name associated with a value that can be changed. It acts as a named storage location in a computer's memory or as a placeholder for a quantity in mathematics.
**Pronunciation:** /ˈvɛəriəbəl/
**Difficulty:** Beginner
**Synonyms:** Identifier, Symbol, Placeholder
**Categories:** Programming Fundamentals, Computer Science, Mathematics
**Tags:** Programming, Data Types, Memory Management, Scope, Declaration, Algebra
Canonical: https://scaleengineer.com/glossaries/variable
---
## Definitions

- **Variable in Computer Programming:** In computer programming, a **variable** is a symbolic name or **identifier** that is associated with a value stored in a computer's memory. This value can be changed or *varied* during the execution of a program. Variables are a fundamental concept, acting as containers for data.

### Key Concepts
*   **Declaration**: The process of creating a variable, which often involves specifying its name and data type (in statically-typed languages). Example in C++: `int score;`
*   **Initialization**: The act of assigning an initial value to a variable at the time of its declaration. Example in Java: `String playerName = "Player1";`
*   **Assignment**: The process of giving a variable a new value after it has been declared. Example in Python: `count = 10` followed later by `count = 11`.
*   **Data Type**: Defines the type of data a variable can hold (e.g., integer, string, boolean, float) and the operations that can be performed on it. In dynamically-typed languages like Python or JavaScript, the type is associated with the value, not the variable itself.
*   **Scope**: The context within which a variable is defined and accessible. A *local variable* is only accessible within a specific function or block, while a *global variable* can be accessed from anywhere in the program.

### Example (Python)
```python
# Declaration and initialization of a variable named 'user_age'
user_age = 25 # An integer variable

# Accessing and using the variable
print(f"The user's age is: {user_age}")

# Re-assignment (updating the variable's value)
user_age = 26
print(f"Next year, the user will be: {user_age}")
```
- **Variable in Mathematics:** In mathematics, a **variable** is a symbol, typically a single letter (like *x*, *y*, or *z*), that represents a quantity that is unknown, unspecified, or can change within the context of a mathematical problem or expression. Unlike in programming, a mathematical variable is an abstract **placeholder** and is not tied to a specific memory location.

### Key Concepts
*   **Independent Variable**: A variable (often *x*) whose value does not depend on any other variable. You can choose its value freely.
*   **Dependent Variable**: A variable (often *y*) whose value depends on the value of one or more other variables.
*   **Parameters/Constants**: Symbols (like *a*, *b*, *c* in `ax² + bx + c = 0`) that are fixed for a specific problem but can change for different problems. They are sometimes called 'constant variables'.

### Example (Algebra)
In the linear equation `y = 3x + 5`:
*   `x` and `y` are variables.
*   `x` is the independent variable. For any value you choose for `x`, you can calculate a corresponding value for `y`.
*   `y` is the dependent variable because its value is determined by the value of `x`.
*   `3` and `5` are constants that define this specific line.

## Etymology

The term 'variable' originates from the Late Latin word 'variabilis', meaning 'changeable' or 'capable of being changed'. This, in turn, comes from the Latin verb 'variare', which means 'to vary' or 'to change'.

## First used

1940s

## Historical context

The concept of a **variable** has its roots in 9th-century mathematics, particularly in the work of the Persian mathematician Al-Khwarizmi, who developed algebra. He used words to represent unknown quantities in equations, laying the groundwork for the symbolic notation we use today.

In the context of computing, the idea evolved significantly. Early computers were programmed using machine code or assembly language, where programmers manipulated data by directly referencing numerical memory addresses. There was no concept of a symbolic **placeholder** like a modern variable.

The major breakthrough came with the development of high-level programming languages in the 1950s. **FORTRAN** (1957) was one of the first languages to introduce the use of symbolic names for memory locations, allowing programmers to write code like `X = Y + Z` instead of dealing with raw memory addresses. This abstraction made programs vastly more readable, maintainable, and portable.

Over time, the concept was refined with the introduction of data types (distinguishing between integers, floating-point numbers, etc.), scoping rules (local vs. global variables), and different storage durations, all of which are central to modern programming paradigms.

## Q&A

- **What is the main difference between a variable and a constant?:** A variable's value can be changed during program execution, whereas a constant's value is fixed after it is defined and cannot be altered.
- **What does the 'scope' of a variable refer to?:** The scope of a variable defines the part of a program where the variable can be accessed or modified. Common scopes include local (within a function) and global (throughout the entire program).
- **What is the difference between declaring and initializing a variable in programming?:** In programming, a variable's declaration is the statement that introduces the variable's name and often its data type to the compiler or interpreter, reserving a place for it in memory. Initialization is the act of assigning it a starting value.

## Usage examples

- In JavaScript, if you declare a **variable** with `let`, its scope is limited to the block it's defined in.
- To track the user's score, we initialized a **variable** named `score` to zero at the start of the game.
- In the algebraic expression `2x - 5`, `x` is a **variable** that can represent any real number.
- A common mistake for beginners is trying to use a **variable** outside of its scope, which results in a reference error.
- The loop counter `i` is a common **identifier** used as a **variable** to track the number of iterations.

## Related terms

- Constant
- Data Type
- Scope
- Memory Address
- Pointer
- Assignment
- Declaration

## Popular related terms

- Constant
- Function
- Data Type
- Array
- Object
