You are on page 1of 8

Revision

Data Types
Data types Storage required
Example
Integer 4 bytes
CustomerID
Mark
Score
Long Integer 8 bytes

Short Integer 2 bytes

Single 4 bytes
AverageNum
Double 8 bytes

Date 8 bytes
DOB
Currency 8 bytes
Amount
Object 4 bytes
Picture
Boolean 1 byte
Paid
String 1 byte for each character
SttudentName

Structure of Procedural Programs
Definitions
Statement: It is a single instruction or step in a program. Usually it is written in one line of code.
Subroutine: It is a set of instructions that perform a specific task as part of a larger program. The main
program contains instruction to call the subroutine



What are the advantages of using subroutines?
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.
Procedure: It is a subroutine that performs a specific task. A procedure may return 0, 1 or many values
to the part of the program from which it was called.
Function: It is a subroutine that executes its statements and always returns a single value.
Example of Function:

Function CalculateRounded (AvMarkAS Double) AS Integer
Dim Rounded AS Integer Rounded = INT(AvMark + 0.5)
CalculateRounded = Rounded
End Function

Parameter: A parameter is a value that is passed (given) to a subroutine (procedure or function).
What is the difference between a procedure and a function?
A Procedure returns 0, 1 or many values whereas a function always returns 1 value

What is recursion?
Recursion is when a function or a procedure contains a call to itself.

Example of Recursion

FUNCTION Factorial(n)
IF n = 1 THEN
RETURN 1
ELSE
RETURN n * Factorial(n 1)
END IF
END

Sequence: It is when the programming statements are executed one after the other, in the order in
which they appear in the program.
Function name
Parameter
Recall function
factorial i.e. recall
itself
Selection: It is a control structure in which there is a test to decide if certain instructions are executed.
Iteration(Loop): It is a control structure in which a group of statements is executed repeatedly either a
set number of times, or until a specific condition is True.
Comparison of different iterations

Nested selection
This is where there is an IF statement contained within an IF statement.
The following algorithm allows a maximum of four attempts to login to a computer system:

INPUT Password
IF NumberOfTries < 5 THEN
IF Password is correct THEN
OUTPUT Successful Login
ELSE
OUTPUT Password was incorrect
END IF
ELSE
OUTPUT You have made too many attempts
END IF





Nested iteration
This is where there is a loop within a loop.
A nested iteration is needed to initialise a two-dimensional array:
FOR row = 0 TO 7
FOR column = 0 TO 5
SET MyArray (row, column) = 0
NEXT column
NEXT row

Trace Table / Dry run
Trace the execution of x = Factorial(3)
01 FUNCTION Factorial(n)
02 IF n = 1 THEN
03 RETURN 1
04 ELSE
05 RETURN n * Factorial(n 1)
06 END IF
07 END

Note that in the above algorithm, the recursion happens on line 05.

What is the difference between iteration and recursion?


Array
An array is a data structure, which allows a set of items of identical data type to be stored together using
the same identifier name.
DIM Names(4) As String
The fiveindividual locations are Names(0), Names(1), Names(2), Names(3), Names(4).




One-dimensional arrays
A one-dimensional array is a data structure in which the array is declared using a single index and can be
visually represented as a list.
The following diagram shows the visual representation of the array Names(4):

Two-dimensional arrays
A two-dimensional array is a data structure in which the array is declared using two indices and can be
visually represented as a table.
The following diagram shows the visual representation of an array Students(4,2):


Each individual element can be referenced by its row and column indices. For example:
Students(0,0) is the data item JONES
Students(2,1) is the item M
Students(1,2) is the item LAMBOURNE

Initialise an Array
Initialising an array is a procedure in which every value in the array is set with starter values this
starting value would typically be for a string array, or 0 for a numeric array

5 rows ( 0 4)
3 columns (0 -2 )
Algorithm for initialising a one-dimensional numeric array:
DIM TestScores(9) As Integer
DIM Index As Integer
FOR Index = 0 TO 9
TestScores(Index) = 0
NEXT


Algorithm for initialising a two-dimensional string array:
DIM Students(4,2) As String
DIM RowIndex, ColumnIndex As Integer
FOR RowIndex = 0 TO 4
FOR ColumnIndex = 0 TO 2
Students(RowIndex,ColumnIndex) =
NEXT
NEXT


Using an array with differing data types
If an array is to be used to store data of different data types, then:
1. The different data must be defined within a Record:
Pseudocode
RECORD Student
Name: String
Gender: Character
Age: Integer
END RECORD

2. The array may now be declared to contain items of data type Record:
DIM MyArray(6) As Student
3. Each record can now be stored as a single item within the array.





VB6
TYPE Sudent
Name As String
Gender As Boolean
Age As Integer
End Type

Examples
Code for calling program
Public Sub cmdRecCalculate2()
Dim Area As Double
Dim side1 As Double
Dim side2 As Double
side1 = InputBox("Enter side 1 value:")
side2 = InputBox("Enter side 2 value:")
Area = RectangleSolution2(side1, side2)
MsgBox "Area is:" & Area
End Sub

Code for called function
Public Function RectangleSolution2(side1value As Double, side2value As Double)
RectangleSolution2 = side1value * side2value
End Function

There are two types of arguments during procedure calls:
1. Call by value (values are passed)
2. Call by reference (address is passed)


File Operations
The following sequential file-related statements and functions have been explained:
Open Prepares a file to be processed by the VB program.
App.Path Supplies the path of your application
Free File Supplies a file number that is not already in use
Input # Reads fields from a comma-delimited sequential file
Line Input # Reads a line (up to the carriage return) from a sequential file
EOF Tests for end-of-file
Write # Writes fields to a sequential file in comma-delimited format
Print # Writes a formatted line of output to a sequential file
Close # Closes a file