T5 Worksheet 5
T5 Worksheet 5
Unit 7 Programming
Task 1
(a) How many parameters does procedure printInfo have?
procedure printInfo(firstname, surname, age)
fullName = firstname + " " + surname
years = str(age) + " years old."
print(fullName)
print(years)
endprocedure
Task 2
Write a function called answerYorN to input and check the response to a question. It should be
“y” or “n”. Any other response is invalid, and the function should ask the user to re-enter until a
valid response is received. The function will then return “y” or “n” when a valid response is
received.
The subroutine is called as follows:
response = answerYorN()
1
Worksheet 5 Procedures and functions
Unit 7 Programming
Task 3
Write a function called addIntegers which takes two integers. The function needs to add up all
the numbers between the first number and last number and return the result.
For example addIntegers(5,10) will return 45
Task 4
1. (a) What is printed out when the following program is run?
function calculateTotal(prices)
total = 0
for i = 0 to prices.length
total = total + prices[i]
next i
return total
endfunction
quantity = 5
itemPrices = [2.00, 2.50, 1.00, 1.00, 1.00]
totalPrice = calculatePrice(itemPrices)
print(totalPrice)
2
Worksheet 5 Procedures and functions
Unit 7 Programming
(b) Name two local variables used inside the calculateTotal function. What is the scope of
each of these variables?
#main program
area = triangle(8, 10)
print(area)