Computer programs use data stores to organise different data within a program.
Data can be constant or variable within programs and functions. When we create a program, we also need to create ways for users to input data into the program and output information from it.
A variable is a named memory location. Data stored in that location changes throughout the duration of the program, for example the high score in a computer game.
When we create a variable, we allocate a space in the memory for it and give it a name.
We give variables names as identifiers to help explain the value assigned to it. For example, age = 16.
We assign values to variables using an assignment operator. For example, = (the 'equals' sign).
We can change the value of our variable at any time during the running of a program. For example, age = age + 1. Here, we have added 1 to the value of the variable 'age'. The age variable will now store the value 17.
A constant is a named location in memory that contains a value, that does not change during the execution of a program. Data is assigned to a constant in the same way as a variable. For example, in a computer game:
CONST CHARACTERSPEED = 5 CONST GRAVITY = -5
When writing code, we often use UPPER CASE to help identify constants.
To get input from a user we have to use an input function. The input function is used to ask the user a question, and invites them to give a response. This value of this user response is then returned as the result of the function, and is stored in a variable. By default, this will be a string, however we can use casting (converting one data type into another) to change the data type. For example:
To output information from a computer program we use a print procedure (this is not used for printing output on to paper). Most programming languages will provide a print procedure for writing or displaying information to the screen. For example: