C8H6 Total points 24/27
The respondent's email (egdorjkhand28@pupils.nlcsjeju.kr) was recorded on submission of
this form.
1. Look at the code below and answer the questions:
(a) How many parameters does procedure printInfo have? 1/1
Feedback
SUBROUTINE printInfo(firstname, surname, age)
The above line of code states all three parameters will be passed to it, in the brackets. This
is where you will always find the parameters of a procedure.
(b) What will be printed by the following call to the procedure?
CALL printInfo("Scooby", "Doo", 52)
Output (2 to tick) Score
ScoobyDoo ···/0
52 ···/0
Scooby ···/0
Scooby Doo 1/1
Doo ···/0
52 years old. 1/1
52years old. ···/0
2.
(a) There are two types of subroutine – functions and procedures. Complete the
table below by ticking whether each feature is available in functions and/or
procedures.
Functions Procedures Score
Have a
subroutine name
which is used to 2/2
call the
subroutine
Take zero, one or
many
parameters
2/2
which are used
to pass values to
the subroutine
Return a value
back to the
1/1
calling
subroutine
The subroutine
can be called
several times in 2/2
the same
program
(b) Look at the function above. What would the program output if the user 1/1
entered "y" (lowercase):
Feedback
The program checks for a "y" or "n" response, any other values are rejected including
capital "Y" or "N". If an invalid repsonse is detected the suer is forced to re-enter until a
valid input is detected, which is then output i.e. "y" or "n" is output.
3. Look at the program above and the function. For the numbers given in 1/1
addIntegers(5,10) it will return and output 45. Study this code and answer
the question below:
What would you get if add addIntegers(9,12)
42
Feedback
Feedback for 2b:
Try the program for yourself, remember to intent the program correctly:
# Function answerYorN checks response is "y" or "n"
def answerYorN():
validAnswer = False
while not validAnswer:
response = input("Please enter y or n: ")
response = response.lower()
if response == "y" or response == "n":
validAnswer = True
return response
#main program
answer = answerYorN()
print(answer)
input("\nPress ENTER to exit program")
Feedback 3:
The function called addIntegers takes two integers, a and b, as parameters. The function
adds up all the numbers between a and b then returns the result. So for 9 and 12 we get
42. The total as it goes looks like this: 9, 19, 30, 42
Try the program for yourself, remember to indent correctly.:
def addIntegers(a, b):
total = 0
for i in range(a, b+1):
total = total + i
return total
#main program
print(addIntegers(5,10))
4. 1/1
(a) What is printed out when the following program is run?
FUNCTION calculateTotal(prices : REAL) RETURNS REAL
.......total ← 0
........FOR i ← 1 TO LENGTH(prices)
................total ← total + prices[i]
........NEXT i
........RETURN total
ENDSUBROUTINE
itemPrices ← [2.00, 2.50, 1.00, 1.00, 1.00]
totalPrice ← calculatePrice(itemPrices)
OUTPUT totalPrice
#........ are used to show indentation
7.50
Feedback
All the elements in itemPrices ← [2.00, 2.50, 1.00, 1.00, 1.00] are added together and then
output, which is 7.50
(b) Two local variables used inside the calculateTotal function. What is the scope
of each of these variables, answer True (T) or False (F) for the statements given for
their scopes?
T F Score
i – scope runs
from start of for
1/1
loop until end of
for loop
total – scope
runs from total
···/0
← 0 to end of
loop
i – scope runs
from start of for
···/0
loop until end of
the function
total – scope
runs from total
0/1
← 0 to end of
the function
Correct answers
T F
total – scope runs from total
← 0 to end of loop
i – scope runs from start of
for loop until end of the
function
total – scope runs from total
← 0 to end of the function
5. Examine the following pseudocode. 1/1
FUNCTION triangle(base : REAL, height : REAL) RETURNS REAL
.......halfBase ← base / 2
.......area ← halfbase * height
.......MISSING CODE
ENDSUBROUTINE
//main program
area ← triangle(8, 10)
OUTPUT area
#........ are used to show indentation
What should go on the missing code line?
RETURN area
Feedback
The return statement is missing in the function. It should read RETURN area - Note,
technically the RETURN statement should be capital.
6. The following is a function:
(a) Which statement or statements below would call this function to find 1/3
the maximum value in an array named NumberOfViewers - Select the one
or ones that would:
findmax(NumberOfViewers)
result ← findmax(NumberOfViewers)
OUTPUT findmax(NumberOfViewers)
Correct answer
findmax(NumberOfViewers)
result ← findmax(NumberOfViewers)
OUTPUT findmax(NumberOfViewers)
Feedback
All would work. They are simply different ways of showing, this code.
(b) Give the name of a parameter used by this function - Must be in *1/1
correct case and spelt correctly.
(score)
Correct answer
score
Feedback
You can see in the brackets the name of the parameter, which is in lowercase:
LENGTH(score)
(c) Give an example of a local variable. - Must be in correct case and spelt 1/1
correctly.
length
Feedback
length, maxvalue, i are all local - score is declared outside the function and is therefore
global.
(d) 1/1
(i) State the data type of score. - Answer must be capital letters and spelt
correctly
INTEGER
Feedback
We know it is a number but without actual data, we cannot ascertain if it is decimal
(REAL/FLOAT) or a whole number (INTEGER)
Read the question - if you got it right but no marks - was it capital and spelt correctly?
(ii) State the data structure of score. 1/1
Array
Feedback
The scores will be stored in an array or list if in Python.
(e) Select the correct examples from this function of different code statements:
Assignment Iteration Selection Score
IF score[i] >
1/1
maxvalue
length ←
1/1
LENGTH(score)
FOR i ← 1 TO
1/1
length
7. The program above uses. a procedure - It asks the user to enter their 1/1
favourite colour. The program then calls a procedure named colour which
takes the entered colour as an input. The procedure will then output one of
the following comments depending on the colour chosen.
What would be a better subroutine to use?
Function
Feedback
By using a procedure, the print statements are in the procedure itself which means it has
less abstraction / it cannot be used in other situations. If it used a function, then the
output could, for example, be concatenated with other strings or processed in some other
way.
NOTE: Case statements could be used instead of the IF statements.
Programs to help understa…
This form was created inside North London Collegiate School Jeju.
Does this form look suspicious? Report
Forms