August 1997
AP207: ADVANCED PROGRAMMING TECHNIQUES

QUESTION 2

Total Marks: 20 Marks

Click here to access other questions

SUGGESTED SOLUTIONS
Solutions and allocated marks are indicated in green.
Return to Question 2

2. (a) Explain the similarities and differences between a function and a procedure. [5]
Similarities
both a function and procedure take a number of arguments as parameters
they both contain a body of code that is executed when the function/procedure is called.
parameter variables referenced in the body of the function/procedure use the values of the parameters that were passed as arguments to the function/procedure.
functions/procedures may contain local variables that have scope during the lifetime of the function/procedure.
if the parameters are reference parameters, then any changes to the parameters are reflected in the data-structures that were passed as the appropriate argument to the function/procedure.
Differences
A function returns a single value as a result.
Function calls are expressions, whereas procedure calls are statements of the language.
1 mark for each of the points above, up to a maximum of 5 marks.
[5 marks]
(b) Identify the differences between call-by-value and call-by-reference parameter passing. [2]
Call-by-value makes a copy of the argument passed as a parameter to the routine. Any changes to the parameter within the body of the routine are not reflected in the variable that was passed to the routine. [1]
In comparison, in a call-by-reference parameter passing scheme, the changes will be reflected in the original variable. [1]
(c) This question is concerned with the implementation of routines that analyse a simple "pass mark" database. The database can contain at most ClassSize records. The various database types you should use in your routines are shown below.

Const ClassSize = 20;
Type MarkRange = 0 . . 100;
DatabaseType = Array [1 . . ClassSize] of MarkRange;
Pass mark database:
(i) Define a function averageMark that takes two arguments: a pass mark database; and an integer n (you can assume that n < ClassSize) that identifies that the first n elements in the database contain student marks. The result from the function should be a Real that represents the average mark. [6]
A sample definition of the average mark function is shown below:
and the following marking scheme should be used:
one mark for a function header with a return value Real.
one mark for a header that contains a DatabaseType, and an integer
one mark for the initialisation of the variable used as the return result to zero;
one mark for a for-loop that iterates over n values;
one mark for incrementing the result variable by each element from the pass mark database
one mark for dividing the sum of the marks by n
do not deduct marks for trivial syntactic errors when making the above points.
[6 marks]
(ii) Define a procedure passFail that takes as its arguments a database of marks and an integer n (you can assume that n < ClassSize) that identifies the number of pass marks in the database, and returns two results: the number of candidates that obtained a pass (i.e., a pass mark > 50); and those that failed. [7]
A sample definition of the pass-fail function is shown below:
and the following marking scheme should be used:
one mark for the procedure header that contains a DatabaseType and an integer with no return type (i.e., the definition of a procedure not a function).
one mark for correct use of Var in header.
one mark for identifying that pass and fail are reference parameters.
one mark for initialising the pass and fail variables to zero;
one mark for defining a for-loop that iterates over n values.
one mark for incrementing pass when the ith element in the database is greater than or equal to 50.
one mark for incrementing fail when the ith element in the database is less than 50.
do not deduct marks trivial syntactic errors when marking the above points.
[7 marks]