Declare and use variables and constants
Variable - A variable is a value that can change during the execution of a program.
Constant - A constant is a value that is set when the program initializes and does not change throughout the program’s execution.
Declaration - A declaration is a statement in a program that gives the compiler or the interpreter information about a variable or constant that is to be used within a program. A declaration ensures that sufficient memory is reserved in which to store the values and also states the variable’s data type. Reserved words/keywords cannot be used as identifier names as this generates a compiler error and comes under the category of syntax error.
Scope -Scope indicates whether a variable can be used by all parts of a program or only within limited sections of the program – for example, within a subroutine.
Features of variables
Holds data in memory
Are assigned a data type
The data can change throughout the program’s operation
The data input must be the same data type as assigned to that variable
Variables are assigned a name that holds data. That name is used to distinguish it from the other variables in your program. The name must not start with a number or character that is not a letter.
The name of the variable must not be a reserved word like PRINT, INPUT, INT, ABS, PAUSE, etc.
Define and use different data types e.g. integer, real, boolean, character and string
Data types and data structures
A data type is a method of interpreting a pattern of bits. Intrinsic data types are the data types that are defined within a particular programming language. There are numerous different data types. They are used to make the storage and processing of data easier and more efficient. Different databases and programming systems have their own set of intrinsic data types, but the main ones are:
Integer
Real
Boolean
String
Character
Container
Integer
An integer is a positive or negative number that does not contain a fractional part. Integers are held in
pure binary for processing and storage. Note that some programming languages differentiate between
short and long integers (more bytes being used to store long integers).
Real
A real is a number that contains a decimal point. In many systems, real numbers are referred to as singles
and doubles, depending upon the number of bytes in which they are stored.
Boolean
A Boolean is a data-type that can store one of only two values – usually these values are True or False.
Booleans are stored in one byte – True being stored as 11111111 and False as 00000000.
String
A string is a series of alphanumeric characters enclosed in quotation marks. A string is sometimes just
referred to as ‘text’. Any type of alphabetic or numeric data can be stored as a string: “Birmingham City”,
“3/10/03” and “36.85” are all examples of strings. Each character within a string will be stored in one byte
using its ASCII code; modern systems might store each character in two bytes using its Unicode. The
maximum length of a string is limited only by the available memory.
Dates
In most computer systems dates are stored as a ‘serial’ number that equates to the number of seconds
since January 1st, 1904 (thus they also contain the time). Although the serial numbers are used for
processing purposes, the results are usually presented in one of several ‘standard’ date formats – for
example, dd/mm/yyyy, or dd MonthName, yyyy. Dates usually take 8 bytes of storage.
Notes: if dates or numbers are stored as strings then they will not be sorted correctly; they will be sorted
according to the ASCII codes of the characters – “23” will be placed before “9”; Telephone numbers must be stored as strings or the leading zero will be lost.
Character
A character is any letter, number, punctuation mark or space, which takes up a single unit of storage
(usually a byte).
Understand and use the concepts of sequence, selection, repetition, totalling and counting
Sequence: Execution of the program usually following the order in which the steps are written.
Selection: A decision between alternative routes through the program that involves at least one condition.
Repetition: A step or sequence of steps repeated within the program as an iteration or loop.
The structure of procedural programs
Statement, subroutine, procedure, function, parameter, loop
Procedural programs are ones in which instructions are executed in the order defined by the programmer.
Procedural languages are often referred to as third generation languages and include FORTRAN, ALGOL,
COBOL, BASIC, and PASCAL.
Statement
A statement is a single instruction in a program, which can be converted into machine code and executed.
In most languages a statement is written on a single line, but some languages allow multiple lines for
single statements. Examples of statements are:
X= “Hello”
A=X+X
Procedure
A procedure is a subroutine that performs a specific task without returning a value to the part of the
program from which it was called.
Function
A function is a subroutine that performs a specific task and returns a value to the part of the program
from which it was called.
Parameter
A parameter is a value that is ‘received’ in a subroutine (procedure or function). The subroutine uses the value of the parameter within its execution. The action of the subroutine will be different depending upon the parameters that it is passed. Parameters are placed in parenthesis after the subroutine name. For example:
Square (5) ‘passes the parameter 5 – returns 25
Square (8) ‘passes the parameter 8 – returns 64
Square(x) ‘passes the value of the variable x
Subroutine/sub-program
A subroutine is a self-contained section of program code which performs a specific task and is referenced
by a name. A subroutine resembles a standard program in that it will contain its own local variables, data types, labels and constant declarations. There are two types of subroutine. These are procedures and functions.
Procedures are subroutines that input, output or manipulate data in some way
Functions are subroutines that return a value to the main program.
A subroutine is executed whenever its name is encountered in the executable part of the main program.
The execution of a subroutine by referencing its name in the main program is termed ‘calling’ the
Subroutine.
The advantage of using procedures and functions are that:
The same lines of code are re-used whenever they are needed – they do not have to be repeated in different sections of the program.
A procedure or function can be tested/improved/rewritten independently of other procedures or functions.
It is easy to share procedures and functions with other programs – they can be incorporated into library files which are then ‘linked’ to the main program;
A programmer can create their own routines that can be called in the same way as any built-in command.