Session XP: 0
Spec 1.1.2, 6.6Paper 1 and Paper 2

Subprograms — Functions and Procedures

Functions and procedures in Python: parameters and arguments, pre-existing library subprograms, and local and global variables.

—
Best score
—
Last attempt
Best attempt progress
0
XP earned
Watch first
Craig 'n' Dave · Edexcel 1CP2Introduction to subprograms (3:59)
Craig 'n' Dave · Edexcel 1CP2How to use pre-existing and user-devised subprograms (9:44)
Craig 'n' Dave · Edexcel 1CP2The Math and Time module (7:02)
Craig 'n' Dave · Edexcel 1CP2The Turtle module (7:17)
Craig 'n' Dave · Edexcel 1CP2Local and global variables (9:47)
Reference & Examples

Why use subprograms? (spec 1.1.2)

  • Avoid repeating code: write it once, call it many times.
  • Break a big problem into smaller parts (decomposition).
  • Each part can be tested separately.
  • Code is easier to read and maintain when names explain what each part does.
  • A team can write different subprograms at the same time.

Procedures and functions in Python — fill in the table

TermDescription
Procedure
Function
Python syntax
Procedure example
Function example

Parameters and arguments — fill in the table

TermDescription
Parameter
Argument
def add(a, b):
result = add(3, 7)
No parameters

Local and global variables (spec 6.6.3) — fill in the table

TermDescription
Local variable
Global variable
Changing a global (the global keyword is not in the PLS)
Why avoid globals
Same name

Pre-existing subprograms (spec 6.6.1) — fill in the table

TypeExamples
Built-in
Library
math
random
time
turtle (not in the PLS, so not set in Paper 2)
User-devised

Why use library subprograms?

  • They are already written and tested, so they save time.
  • They are usually more reliable than code written from scratch.
  • They give access to things that are hard to write, such as random numbers.
  • Remember the import line, or Python will report an error.
Procedure: no return value
def greet(name):
    print("Hello, " + name + "!")
    print("Welcome to the program.")

greet("Ander")
# Hello, Ander!
# Welcome to the program.
Function: returns a value
def calculate_area(length, width):
    area = length * width
    return area

room_area = calculate_area(5, 3)
print(room_area)                              # 15
print("Area = " + str(calculate_area(4, 7)))  # Area = 28
Function with a local variable
def is_prime(n):
    if n < 2:
        return False
    for i in range(2, n):      # i is local to this function
        if n % i == 0:
            return False
    return True
Changing a global variable (extra: the global keyword is not in the PLS)
score = 0                # global variable

def add_point():
    global score         # needed to change the global
    score = score + 1

add_point()
print(score)             # 1
Exam practice — 7 questions · 17 marks · 1CP2 Paper 2 style
⚡ In the real Paper 2 you type and run Python on screen. Here, type your Python answer and the AI marks it against the mark points. Check the Programming Language Subset (PLS) you are given in the exam for the exact commands.
1 markProcedure vs functionPaper 2 · spec 6.6.2

What is the key difference between a function and a procedure?

A Functions run faster than procedures
B A function returns a value; a procedure does not
C Procedures can have parameters but functions cannot
D Procedures are defined with def; functions are not
4 marksWrite a functionPaper 2 · spec 6.6.2

Write a function called max_value that takes two parameters, a and b, and returns the larger of the two. Then show how to call it with 15 and 23 and print the result. (4 marks)

Hint: def, two parameters, compare, return, then call it.
+40 XP
1 markLocal scopePaper 2 · spec 6.6.3

A variable called total is created inside a function. What happens to it when the function finishes?

A It becomes a global variable
B It is destroyed, so it does not exist outside the function
C It keeps its value and can be used anywhere
D It is automatically returned
4 marksIdentify and fix the problemPaper 2 · spec 6.6.3

The program below should print 10. Explain what is wrong, say what is printed, and write the corrected version. (4 marks)

def double(n):
    result = n * 2

total = double(5)
print(total)
Hint: What does a function give back if it has no return statement?
+40 XP
1 markParametersPaper 2 · spec 6.6.2

In the definition def power(base, exponent):, what are base and exponent?

A Arguments
B Parameters
C Global variables
D Return values
3 marksLibrary subprogramsPaper 2 · spec 6.6.1

A program must pick a random whole number from 1 to 6 (a dice roll) and print it. Write the Python, including any import. (3 marks)

Hint: The random module is not available until you import it.
+30 XP
3 marksGlobal and local variablesPaper 2 · spec 6.6.3

Explain why this program gives an error, and how to fix it. (3 marks)

count = 0

def add():
    count = count + 1

add()
Hint: Python treats a variable that is assigned inside a subprogram as local.
+30 XP
Quick recall flashcards

Module complete! 🎉

Score loading...

⚡ +10 XP