GCSE · Computer Science · OCR · Spec J277

Sub programs: procedures

Typing the same menu out ten times, then fixing every copy when one option changes? No thanks. Write it once, name it, and call it whenever you need it.

Programming · Define, then call

One menu, two calls

Step through this Python program. Watch which line is running, what the parameter player holds, and what appears in the output.

Predict before you step: on which step will Welcome, Asha first appear, and how many times will the menu be shown?

1 def show_menu(player):
2 print("Welcome, " + player)
3 print("1. Play")
4 print("2. Quit")
5 # main program
6 show_menu("Asha")
7 show_menu("Ben")

Variables

player=—

Output

 

Step 1: Python reads the def line and the indented lines under it. That defines a procedure called show_menu with one parameter, player. player has no value yet, and the output box is empty: defining the procedure hasn't run it.

1 / 11

Use Next and Back. Keep an eye on the line marker: it jumps from the call into the procedure.

Step 1 of 11: Python reads the def line and the indented lines under it. That defines a procedure called show_menu with one parameter, player. player has no value yet, and the output box is empty: defining the procedure hasn't run it..

Watch out: Step 1 prints nothing. Reading the def block only defines the procedure; the output starts when the main program calls it.

Programming · Defined or run?

What does this program print?

A program has just two lines: def say_hello(): and, indented underneath it, print("Hello!"). That's the whole program.

Which is closest to what you think right now?
How sure are you?

Programming · Which kind of subprogram?

Procedure or function?

Does this subprogram return a value to the program, or not?

Still to sort

Procedure (0)

Runs its commands when called. Returns no value.

Where the line is: Doing something useful, like displaying text, doesn't make it a function. If nothing is returned, it's a procedure.

Function (0)

A subprogram that also returns a value.

Where the line is: Doing a calculation isn't the test. Returning a value to the calling program is.

5 of 5 still to sort.

Each of these is a subprogram. Decide which kind it is, then read why.

Programming · Arguments and parameters

Find the faulty call

A student wrote this program to display two players' scores. One line breaks the rule for calling a procedure. Which one?

A student's program — which line goes wrong?

Why bother with procedures?

?

Reason it through

Why put a game's menu code in a procedure instead of typing it out every time it's needed?

Link 1 of 3

First link · your turn

The menu code goes into a procedure called show_menu. What can the main program now do whenever it needs the menu?

2
Locked — reveal the link above first
3
Locked — reveal the link above first

WHAT YOU'VE LEARNED

A quick recap of today's lesson.

A named block of code that sits quietly until the main program calls it, then does its job as often as you ask.

What you need to know

  • A subprogram is a section of code that does one specific job, and the main program can call it whenever it's needed.
  • A procedure runs its commands when it's called but returns no value; a function is a subprogram that also returns a value.
  • print() is a procedure you already know, and you can write your own, like one that displays a menu.
  • Have a goYour mate is certain print() is a function, "because it actually does something". Using the rule you've just read, set them straight.

    It's a procedure: it carries out its job but returns no value.

    Doing something isn't the test, because procedures and functions both do things. Only a function also hands a value back to the program.

  • Repeated code like a menu belongs in a procedure: call it as many times as you like, and change it just once.
  • In Python, a procedure starts with def, then its identifier (a meaningful name you choose), then any parameters in brackets.
  • Defining a procedure doesn't run it. Nothing happens until the main program calls it.
  • Have a goJas writes a perfect def block for a procedure called cheer, presses Run and gets... nothing. "Python's broken!" What's really missing?

    A line in the main program that calls cheer.

    Python's fine. The def block only set the procedure up; without a call, its commands never run.

  • Parameters define the data a procedure needs when it's called, and inside the procedure they're treated as local variables.
  • A call is the procedure's name followed by the arguments; each argument's value is passed into a parameter.
  • Argument and parameter names don't have to match, but there must be the same number of arguments as parameters.
  • Have a goA procedure is defined as def party(host, guest, food):. How many arguments must every call to it give?

    Three.

    Three parameters need three arguments. What the arguments are called doesn't matter; how many there are does.

The big picture

A subprogram is a section of code that does one specific job and can be called whenever the main program needs it. Procedures run their commands but return no value; functions also return a value. In Python you define a procedure with def, its identifier and any parameters in brackets, but defining it doesn't run it: the main program has to call it, giving one argument for each parameter.

Key points

1A subprogram does one specific job and can be called whenever the main program needs it.
2A procedure returns no value; a function also returns a value.
3A procedure is defined with def, an identifier and any parameters in brackets, and only runs when it's called.
4A call passes argument values into the parameters: same number of each, names free to differ.
5Repeated code in a procedure is written once, called many times and changed in one place.

Worked example

Problem

A Python program defines greet_both(first, second), which prints "Hi " + first and then "Hi " + second. The main program sets a = "Kai" and b = "Zoe", then calls greet_both(b, a). What does the program print?

⚠ Watch out

Thinking that writing a def block runs the procedure. It doesn't: the procedure only runs when the main program calls it, so a program with a definition and no call prints nothing from it.

🧠

Memory hook

A def block is a recipe card filed in a drawer: nothing gets cooked until someone calls for it by name and hands over the ingredients. One card, one ingredient for each blank, as many meals as you like.

✓

Check yourself

Cover the page. Using def show_menu(player): and the call show_menu("Asha") as your example, explain the difference between a parameter and an argument, and say what happens to "Asha" when the call runs.

Flashcards

(11)
What is a subprogram?
A section of code that does one specific job and can be called whenever the main program needs it.
What are the two kinds of subprogram?
Procedures and functions.
Procedure vs function: what's the difference?
A function returns a value to the program that called it. A procedure runs its commands but doesn't return a value.
Name a procedure you've already used.
print()
Why put a menu in a procedure?
It can be called as many times as needed, and the options only have to be changed in one place.
Which keyword starts a procedure definition in Python?
def, followed by the procedure's identifier and any parameters in brackets.
In a def line, what is the identifier?
The meaningful name the programmer chooses for the procedure.
When does the code inside a procedure run?
Only when the procedure is called by the main program, not when it's defined.
What do parameters do?
They define the data that must be passed to a procedure when it's called. Inside the procedure they're treated as local variables.
What does a procedure call consist of?
The procedure's name followed by the arguments to pass to it.
Must an argument have the same name as its parameter?
No. The names can differ, but there must be the same number of arguments as parameters.

Tap any card to flip it, or use Study as deck to go through them one at a time. In the full lesson these run as a spaced-repetition deck — you rate each card Hard, Good or Easy and the tricky ones keep coming back until they stick.

Learning with Lightbulb is opening soon

You can use this lesson now. Join the waitlist and we'll let you know when the full Lightbulb experience is ready.

Keep me posted

More OCR GCSE Computer Science topics

See the full OCR Computer Science curriculum →

How this lesson was checked. This OCR GCSE Computer Science (specification J277)lesson was published through Lightbulb Learning's human-designed editorial process — the educational standards, accuracy rules and publication checks it must pass were authored and approved by Philip Halpin. It passed subject-specific assessment, automated educational checks and technical publication verification before going live (publication checks completed 3 November 2026). Published pages are monitored, human spot-checking is ongoing across the lesson library, and anything found wrong is corrected or withdrawn. How our lessons are made and checked. Spotted a mistake? Email hello@lightbulblearning.co and we'll review it.