Lesson

Functions and subprograms

A PRIMM lesson on grouping code, using parameters, and returning values clearly.

Open a shared Python workspace in seconds.

Lesson overview

Topic
Subprograms
Level
GCSE
Learning objective
Explain how parameters and return values help organise code into clear reusable subprograms.

1.Predict

Example code

def area(length, width):
    result = length * width
    return result

room_area = area(4, 6)
print(room_area)
  • What value will room_area store after the function call?
  • What role do the parameters length and width play in the function?

2.Run

Run the code in Paired, then change the values passed into the function and observe the result.

3.Investigate

  • The function groups a short calculation into one named block.
  • The return statement sends a value back to the line that called the function.
  • Why is return useful here instead of printing inside the function?
  • How would the result change if different values were passed in?

4.Modify

  • Change the function so it calculates perimeter instead of area.
  • Rename the function and variables so the purpose is even clearer.

5.Make

Write your own function that takes two inputs and returns one useful result for a simple problem.

Extension

Create a second function that reuses the first result in a larger calculation.

Need extra support?

Reece offers GCSE & A-Level Computer Science tutoring: reece-tutoring.co.uk