15-procedures.py
python-programming / subprograms  ·  Lesson 16
Lesson 16 — Subprograms

Subprograms: Procedures

Package reusable instructions that perform a task without returning a value.

A procedure is a named subprogram used to carry out a task. Python uses def for subprograms; a procedure-style subprogram simply does not return a result for use elsewhere.

Predict

Read the code first. Do not run it yet.

procedures-predict.py
def greet(name):
    print("Hello", name)

greet("Mina")
greet("Leo")
Predict before running
  • How many times is the subprogram called?
  • What value is passed into the parameter name each time?
  • What is printed?

Investigate

Use this code to explain exactly how the program works.

procedures-investigate.py
def show_total(a, b):
    total = a + b
    print(total)

show_total(4, 7)
Investigate the code
  • Which names are parameters?
  • Which variable is local to the subprogram?
  • Does the subprogram return a value to the calling code?

Defining and calling a procedure

procedure.py
def display_message(message):
    print("***", message, "***")

display_message("Ready")
Output
*** Ready ***
How it works
  • def gives the subprogram a name.
  • The parameter receives a value when the subprogram is called.
  • The indented body runs each time the name is called.
Python uses functions for both rolesPython does not have a separate procedure keyword. A def block with no returned result is used as a procedure.

Self-study guide

A procedure is a subprogram that performs a task but does not return a value for later use. OCR Exam Reference Language has a separate procedure keyword. Python has no separate procedure keyword: both procedures and functions are written with def.

In Python we call a def block procedure-style when its purpose is to perform actions such as output and it does not return a useful result. Python will technically return None if no explicit return value is used, but that is not the intended result of a procedure-style subprogram.

OCR focusUnderstand the use of procedures and where they are appropriate. Translate OCR procedure ... endprocedure into a Python def that performs a task without returning a useful value.

OCR Exam Reference Language ↔ Python

OCR Exam Reference Language / termPythonWhat to remember
procedure printName(name) ... endproceduredef print_name(name): ...Both define a reusable subprogram with a parameter.
printName("Maya")print_name("Maya")Procedure call.
procedure does not return a resultPython `def` with no useful `return` valueUse when the main purpose is to perform an action.
Read both formsIn an examination you may see OCR Exam Reference Language even when you have learned the idea in Python. Translate the structure, not just the symbols.

Worked example

This procedure produces consistent output for any player passed to it.

lesson-16-worked.py
def show_player(name, level):
    print("Player:", name)
    print("Level:", level)

show_player("Nova", 7)
show_player("Mina", 12)
Output
Player: Nova
Level: 7
Player: Mina
Level: 12
Work through it line by line
  • The def statement creates the subprogram.
  • name and level are parameters: names used to receive values for a call.
  • The indented body runs only when the procedure-style subprogram is called.
  • Two calls reuse exactly the same output logic with different arguments.

Another example

A procedure can contain selection or iteration learned earlier.

lesson-16-example-2.py
def check_age(age):
    if age < 17:
        print("Too young")
    else:
        print("Old enough")

check_age(18)
Output
Old enough
Why this example matters
  • The parameter supplies the input data to the subprogram.
  • The procedure performs the decision and output itself.
  • No value needs to be returned to the caller.

Common mistakes

Avoid these errors
  • Saying Python has a procedure keyword. It does not.
  • Adding return automatically even when the caller does not need a result.
  • Forgetting to indent the procedure body.
  • Confusing a parameter name in the definition with an argument value in a call.
Exam-safe warningOCR distinguishes procedures and functions explicitly. Python uses def for both, so decide by purpose: action/no useful return = procedure-style; returned result = function.

Exam-ready checklist

You should now be able to
  • Explain what a procedure is and why it improves structure.
  • Define and call procedure-style Python subprograms.
  • Use parameters correctly.
  • Translate between OCR procedure syntax and Python def.

What you have learned

  • A procedure is a reusable named subprogram.
  • Parameters pass data into a subprogram.
  • Python uses def to define procedure-style subprograms.
  • A procedure performs a task without returning a result for use by the caller.