3HLLConstructs

advertisement
Higher Grade Computing Studies
3. High Level Language Constructs
Simple Data Types
• Integer: An integer value may be any positive or negative whole
number, e.g. 24, -6.
• Real: A real value is taken to mean a number with a fractional part,
e.g. 3.65.
• String: A string is any single or group of characters, e.g. “Peter”
• Boolean: A boolean value is a two-state value. It can either be True
or False
1
Higher Computing
Software Development
S. McCrossan
Higher Grade Computing Studies
3. High Level Language Constructs
Structured Data Types
• Array: An array is a set of data all sharing the same variable name
which can be accessed by means of a subscript.
2
Higher Computing
Software Development
S. McCrossan
Higher Grade Computing Studies
3. High Level Language Constructs
Modularity
• Modularity is a feature of well written code and it means to divide
a larger program into parts that work together to complete the
program’s task.
• Subroutines (also known as subprograms or procedures) and
functions allow programs to be written in modular form.
3
Higher Computing
Software Development
S. McCrossan
Higher Grade Computing Studies
3. High Level Language Constructs
Modularity
• Procedures are sections of code which do a specific set of tasks in
the program, and which have been grouped separately so that they
are called (brought into use) during the running of the program.
• A function is similar to a procedure in that it is also a section of
code which does a specific task in the program, and which is called
(brought into use) during the running of the program.
• The difference between a procedure and a function is that:
• a procedure produces an effect, whereas
• a function produces a single value.
4
Higher Computing
Software Development
S. McCrossan
Higher Grade Computing Studies
3. High Level Language Constructs
Parameters
• Parameters are used to pass data to/from subprograms. The
statement :
calculate_volume(l, b, h)
means that the subprogram calculate_volume uses as its data the
variables l, b and h.
• l,b and h are known as the actual parameters because they are
the ones passed into the procedure.
5
Higher Computing
Software Development
S. McCrossan
Higher Grade Computing Studies
3. High Level Language Constructs
Parameters
• The procedure to do the calculation will start like this :
procedure calculate_volume(length,breadth,height)
• length,breadth and height are known as the formal parameters
because they are the ones used by the procedure.
6
Higher Computing
Software Development
S. McCrossan
Higher Grade Computing Studies
3. High Level Language Constructs
Parameters
• Value parameters result in a copy of the original. They cannot
be passed out of the subprogram and used elsewhere. They are
safer to use as the original is unaffected and so it cannot be
accidentally altered.
• If a value parameter has its value changed within a
procedure, this change of value will not be passed back out
of the procedure. It will revert back to the value it had
before entering the procedure.
7
Higher Computing
Software Development
S. McCrossan
Higher Grade Computing Studies
3. High Level Language Constructs
Parameters
• Reference parameters alter the value of the variables
themselves. They can be passed out and used elsewhere but can
result in accidental alteration of a variable.
• If a reference parameter has its value changed within a
procedure, this change of value will be passed back out of
the procedure.
8
Higher Computing
Software Development
S. McCrossan
Higher Grade Computing Studies
3. High Level Language Constructs
Scope of variables
Local Variables
• Local variables are variables which exist only within a
subprogram. They cannot be accessed or assigned a value except
within that subprogram.
Global Variables
• Global variables are variables which are used in any and every part
of a program, including subprograms. They should always be used
with care and preferably should not be used within subprograms if at
all possible.
9
Higher Computing
Software Development
S. McCrossan
Higher Grade Computing Studies
3. High Level Language Constructs
Scope of variables
The scope of a variable is the parts of the program it can be used in.
If a variable is used throughout the whole program, both in the main
program and procedures, then its scope is global. If the variable,
however, can only be used within one procedure then its scope is
only within that procedure.
10
Higher Computing
Software Development
S. McCrossan
Higher Grade Computing Studies
3. High Level Language Constructs
String Operations
• Concatenation is a process where a
string is joined to another string to
make a larger string, e.g. “sauce” +
“pan” = “saucepan”.
11
Higher Computing
Software Development
S. McCrossan
Higher Grade Computing Studies
3. High Level Language Constructs
String Operations
• Substrings are parts of larger strings
that can be ‘pulled out’ and used.
12
Higher Computing
Software Development
S. McCrossan
Higher Grade Computing Studies
3. High Level Language Constructs
Formatting Input and Output
• Languages can have a range of sophisticated input methods
(ways of letting the user enter data) such as text boxes, radio
buttons, spin boxes, drop down menus, etc, or it may just be
limited to keyboard input.
• You may also be able to change the way text looks on screen by
altering the colour, font, size, style and alignment of text.
13
Higher Computing
Software Development
S. McCrossan
Higher Grade Computing Studies
3. High Level Language Constructs
CASE statements
• The CASE statement allows for multiple outcomes. It
removes the need for many IF…THEN…ELSE statements and
is clearer for the programmer, or whoever is reading the
programming code, to understand.
14
Higher Computing
Software Development
S. McCrossan
Download