Edexcel GCSE Computer Science (1CP2) · Problem solving with programming
Practise Subprograms and libraries. 21 exam-style questions plus unlimited generated ones on this subtopic, at up to four difficulty levels, with full mark schemes and a progress tracker. Free, no account needed.
State the smallest value and the largest value that the program could output.
Smallest 1, largest 6
Give a reason why double and addTen are functions and not procedures.
They each return a value to the code that called them.
Write a procedure showStars(n) that outputs a line of n stars.
def showStars(n):
print("*" * n)
Sample questions
Written for this site in the style of Edexcel exam questions. They are not taken from real past papers.
Question 1Easy5 marks
Programs are often written using subprograms.
(a) Define the term 'subprogram'.[1]
(b) Identify the built-in Python function.[1]
len()
calculateTotal()
getInitials()
showMenu()
(c) Give two benefits of using subprograms when writing a program.[2]
(d) State the keyword used to start the definition of a subprogram in Python.[1]
Show the answer and mark scheme
(a)Answer: A named, self-contained block of code that carries out a specific task and can be called from other parts of the program.
a named / self-contained block of code that carries out a specific task (and can be called / used from elsewhere in the program)
(b)Answer:len()
(c)Answer: The code can be called many times without being written out again, and each subprogram can be tested on its own.
code can be reused / called many times without repeating it (so the program is shorter)
each subprogram can be tested / debugged separately
the program is easier to read / maintain (a change is made in one place)
different programmers can work on different subprograms at the same time
subprograms can be reused in other programs / stored in a library
(d)Answer:def
def
Question 2Medium7 marks
Look at this program.
01 def double(n):
02 return n * 2
03
04 def addTen(n):
05 return n + 10
06
07 x = double(3)
08 y = addTen(x)
09 z = double(addTen(1))
10 print(x, y, z)
(a) State the output of the program.[3]
(b) Give a reason why double and addTen are functions and not procedures.[1]
(c) State the value of double(double(double(1))).[1]
(d) Write a function triple(n) that returns three times the value of its parameter.[2]
Show the answer and mark scheme
(a)Answer:6 16 22
6 (x)
16 (y)
22 (z)
(b)Answer: They each return a value to the code that called them.
they return a value (to the code that called them)
(c)Answer: 8
8
(d)Answer:
def triple(n):
return n * 3
a def line for triple with one parameter
returns (not prints) the parameter multiplied by 3
Question 3Hard8 marks
A museum charges £12.50 for each adult ticket and £6.00 for each child ticket. Members get 10% off the total cost.
(a) Write a Python function calculateCost(adults, children, member) that returns the total cost of the tickets in pounds. The parameter member is True if the customer is a member and False if not.[4]
(b) State the value returned by calculateCost(2, 3, True).[1]
(c) Write the main program. It must input the number of adults, the number of children and whether the customer is a member (Y or N). It must then call calculateCost and output the cost rounded to 2 decimal places.[3]