## Definition A named location in [[random-access memory|memory]] whose value can change while the program is running. *Noun.* > `username` is a variable that refers to the memory location used to store the username. ## Characteristics - Has an [[identifier]]; - can be [[declare|declared]]—an identifier is given and memory reserved; - can be [[initialise|initialised]]—given an initial value at the point of declaration or later; - its value can be updated using the `=` [[assign|assignment]] [[operator]]; and - its [[data type]] could be changeable (depending on the programming language). ## Examples ### Variable with primitive data type Variables can have values that can be [[primitive data type|primitive data types]]. ```python age = 16 # variable with identifier 'age' and value 16 (an integer) height = 175.2 # variable with identifier 'height' and value 175.2 (a float) ``` ### Variable with a value returned by a function Variables can have values provided by a [[function]] return. ```python name = input("What is your name?") # variable with identifier 'name' name_length = len(name) # variable with identifier 'name_length' ``` ### Variable with a data structure value Variables can have values that are [[data structure|data structures]]. ```python numbers = [3, 1, 4, 1, 5, 9] # a list account = BankAccount() # an object ``` ### Variable with a pointer value (C) Variables can have values that are [[pointer|pointers]]. ```C int age = 16; // variable with identifier 'age' and value 16 (an int) int *p_age = &age; // variable with identifier 'p_age' and value: the memory address of 'age' (a pointer) printf("%d\n", age); // prints 16 printf("%p\n", p_age); // prints the memory address, e.g. 0x7ffee4d2a9bc printf("%d\n", *p_age); // prints 16—dereferencing p_age gives the value stored at that address ``` ## Non-examples ### Constant A [[constant]] is not a variable—the value of a constant cannot change while the program is running. ### Value A value is not a variable. The memory location which the variable refers to may store a value, but the variable is not itself a value—in the same way that a person may have a cat, but not themselves be a cat. ### Pointer A [[pointer]] is an example of a value (specifically, a memory address). A variable's value may be a pointer, but the variable is not itself a pointer.