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.
def greet(name):
print("Hello", name)
greet("Mina")
greet("Leo")- How many times is the subprogram called?
- What value is passed into the parameter
nameeach time? - What is printed?
Investigate
Use this code to explain exactly how the program works.
def show_total(a, b):
total = a + b
print(total)
show_total(4, 7)- 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
def display_message(message):
print("***", message, "***")
display_message("Ready")*** Ready ***
defgives the subprogram a name.- The parameter receives a value when the subprogram is called.
- The indented body runs each time the name is called.
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.
procedure ... endprocedure into a Python def that performs a task without returning a useful value.OCR Exam Reference Language ↔ Python
| OCR Exam Reference Language / term | Python | What to remember |
|---|---|---|
procedure printName(name) ... endprocedure | def print_name(name): ... | Both define a reusable subprogram with a parameter. |
printName("Maya") | print_name("Maya") | Procedure call. |
procedure does not return a result | Python `def` with no useful `return` value | Use when the main purpose is to perform an action. |
Worked example
This procedure produces consistent output for any player passed to it.
def show_player(name, level):
print("Player:", name)
print("Level:", level)
show_player("Nova", 7)
show_player("Mina", 12)Player: Nova Level: 7 Player: Mina Level: 12
- The
defstatement creates the subprogram. nameandlevelare 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.
def check_age(age):
if age < 17:
print("Too young")
else:
print("Old enough")
check_age(18)Old enough
- 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
- Saying Python has a
procedurekeyword. It does not. - Adding
returnautomatically 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.
def for both, so decide by purpose: action/no useful return = procedure-style; returned result = function.Exam-ready checklist
- 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
defto define procedure-style subprograms. - A procedure performs a task without returning a result for use by the caller.