Back to the topic

The mark scheme starts on a new page, so you can print pages one and two for a student and keep the last page yourself. Everything prints in black on white whichever theme you are using.

Python · GCSE 3

Subprograms: Procedures, Functions and Parameters

Section A: 10 multiple choice questions, 10 marks. Section B: 5 written questions, 16 marks. Total 26 marks.

Name

Class

Date

Mark  / 26

Section A

Circle the letter of the correct answer. One mark each.

  1. 1What is the difference between a function and a procedure?1
    1. AA procedure can only be called once
    2. BA procedure returns a value and a function does not
    3. CA function uses parameters and a procedure cannot
    4. DA function returns a value and a procedure does not
  2. 2In def triple(number): and the call triple(7), what is 7?1
    1. AAn argument
    2. BA parameter
    3. CA local variable
    4. DA return value
  3. 3What does a Python function that has no return statement give back?1
    1. AAn error
    2. BNone
    3. CAn empty string
    4. D0
  4. 4Which keyword lets a subprogram change a global variable?1
    1. Areturn
    2. Bglobal
    3. Cconst
    4. Dimport
  5. 5Why are local variables preferred to global ones?1
    1. AThey run faster in every case
    2. BThey are easier to type
    3. CThey cannot be changed by accident by other parts of the program
    4. DThey are the only ones that can be numbers
  6. 6def f(x): return x * 2. What does print(f(f(3))) display?1
    1. A3
    2. B6
    3. C9
    4. D12
  7. 7Where can a local variable be used?1
    1. AOnly in the main program, never inside a subprogram
    2. BOnly inside the subprogram where it is created
    3. CInside any subprogram that is called by the one that created it
    4. DAnywhere in the program after the line that created it
  8. 8Which is a benefit of using subprograms?1
    1. AThe program needs no testing
    2. BVariables no longer need names
    3. CCode can be reused instead of copied
    4. DThe program always runs faster
  9. 9What does random.randint(1, 6) produce?1
    1. AA whole number from 1 to 6, including both
    2. BA decimal number somewhere between 1 and 6
    3. CA whole number from 1 to 5, because the top value is excluded
    4. DA whole number from 0 to 6, including 0 and 6
  10. 10A function ends with return a, b. What does it send back?1
    1. AOnly the first value
    2. BOnly the last value
    3. CAn error, because return takes one value
    4. DBoth values, which can be unpacked into two variables

Section B

Answer in the space provided. The number of marks tells you how many separate points to make.

  1. 1StateState two benefits of using subprograms in a program.2
  2. 2WriteWrite a Python function called circle_area that takes a radius as a parameter and returns the area of the circle, rounded to 2 decimal places. The area is pi multiplied by the radius squared.3
  3. 3ExplainExplain the difference between a local variable and a global variable.3
  4. 4ExplainThe program shown produces an error. Explain why, and describe two ways it could be corrected.4
    Python
    total = 0
    
    def add(amount):
        total = total + amount
    
    add(5)
    print(total)
  5. 5WriteWrite a procedure called draw_box that takes a width and a height as parameters and displays a rectangle made of asterisks with that many columns and rows. For example, a width of 5 and a height of 3 displays three rows of five asterisks.4

Mark scheme

Section A

1D2A3B4B5C6D7B8C9A10D

Section B

1 State two benefits of using subprograms in a program. 2

One mark for each of the following, to a maximum of 2.

  • Code can be reused without being written again
  • Each subprogram can be tested separately
  • Makes the program easier to read or maintain
  • Different programmers can write different subprograms at the same time

Model answer. Subprograms let the same code be reused by calling it, rather than copying it each time it is needed. They also make a program easier to test, because each subprogram can be checked on its own with known values, and easier to read and maintain, because a fault only has to be fixed in one place.

2 Write a Python function called circle_area that takes a radius as a parameter and returns the area of the circle, rounded to 2 decimal places. The area is pi multiplied by the radius squared. 3

One mark for each of the following, to a maximum of 3.

  • Defines a function with a parameter for the radius
  • Calculates pi * radius squared correctly
  • Rounds to 2 decimal places and returns the result

Model answer.

Python
import math

def circle_area(radius):
    return round(math.pi * radius ** 2, 2)

print(circle_area(3))

The function takes the radius as a parameter, calculates pi times the radius squared and rounds it to two decimal places. It uses return, so the caller can use the value. The call shown prints 28.27.

3 Explain the difference between a local variable and a global variable. 3

One mark for each of the following, to a maximum of 3.

  • A local variable is declared inside a subprogram
  • A local variable can only be used in that subprogram and is lost when it ends
  • A global variable is declared outside subprograms and can be used throughout the program

Model answer. A local variable is declared inside a subprogram. It can only be used within that subprogram and it stops existing when the subprogram finishes. A global variable is declared outside all subprograms, so it can be used from any part of the program, and it exists for as long as the program is running.

4 The program shown produces an error. Explain why, and describe two ways it could be corrected. 4

One mark for each of the following, to a maximum of 4.

  • Assigning to total inside the function makes it a new local variable
  • The local total has no value when total + amount is worked out, so there is an error
  • Correction one: declare total as global inside the function
  • Correction two: pass total in as a parameter and return the new value

Model answer.

Because the function assigns to total, Python treats total as a new local variable. The right hand side then uses that local variable before it has been given a value, so the program stops with an error. It can be corrected by declaring the variable global inside the function:

Python
total = 0

def add(amount):
    global total
    total = total + amount

add(5)
print(total)

A better correction is to pass the value in and return the result, so the function does not depend on a global at all:

Python
def add(total, amount):
    return total + amount

total = add(0, 5)
print(total)

5 Write a procedure called draw_box that takes a width and a height as parameters and displays a rectangle made of asterisks with that many columns and rows. For example, a width of 5 and a height of 3 displays three rows of five asterisks. 4

One mark for each of the following, to a maximum of 4.

  • Defines a subprogram with two parameters, width and height
  • A loop that repeats height times, one per row
  • Displays width asterisks on each row
  • Does not return a value, because it is a procedure

Model answer.

Python
def draw_box(width, height):
    for row in range(height):
        print("*" * width)

draw_box(5, 3)

The loop runs once for each row. Multiplying a string by a number repeats it, so "" width makes one row of width asterisks, and print moves to a new line. It displays the box rather than returning anything, so it is a procedure.