16-functions.py
python-programming / subprograms  ·  Lesson 17
Lesson 17 — Subprograms

Subprograms: Functions

Return a value from a reusable subprogram.

A function is a subprogram that returns a value. The returned value can be assigned to a variable or used directly in an expression.

Predict

Read the code first. Do not run it yet.

functions-predict.py
def double(number):
    return number * 2

answer = double(6)
print(answer)
Predict before running
  • What value is passed into number?
  • What value is returned?
  • What is stored in answer?

Investigate

Use this code to explain exactly how the program works.

functions-investigate.py
def maximum(a, b):
    if a > b:
        return a
    else:
        return b

print(maximum(9, 4))
Investigate the code
  • Trace which branch runs.
  • Why does return make this a function rather than a procedure-style subprogram?
  • What would maximum(3, 8) return?

Returning a value

area.py
def rectangle_area(length, width):
    area = length * width
    return area

result = rectangle_area(8, 5)
print(result)
Output
40
How it works
  • Arguments 8 and 5 are passed into the parameters.
  • The function calculates a local value.
  • return sends the result back to the calling code.

Choosing a procedure or a function

UseBest choiceExample
Carry out an action when the caller does not need a result backProcedure-style subprogramDisplay a menu or print a message
Calculate or obtain a value that the caller needs to useFunctionCalculate an average or return a maximum value
The deciding questionIf the calling code needs a value back, use a function with return. If the subprogram only needs to perform an action, a procedure-style subprogram is appropriate.

Self-study guide

A function is a subprogram that returns a value to the code that called it. The return value can be stored, printed or used in a larger expression. Both OCR Exam Reference Language and Python use return for this purpose.

The key distinction from a procedure is not whether the subprogram contains output, but whether the caller needs a result back. A well-designed calculation is often a good candidate for a function.

OCR focusUnderstand functions, call them with parameters, use their return values, and know when a function is more appropriate than a procedure.

OCR Exam Reference Language ↔ Python

OCR Exam Reference Language / termPythonWhat to remember
function squared(number) ... return squared endfunctiondef squared(number): ... return resultFunction definition and returned result.
newValue = squared(4)new_value = squared(4)Store the returned result for later use.
print(squared(4))print(squared(4))A returned value can be used directly.
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

A function can hide a calculation behind a meaningful name.

lesson-17-worked.py
def time_in_seconds(hours, minutes, seconds):
    total = hours * 60 * 60 + minutes * 60 + seconds
    return total

result = time_in_seconds(1, 15, 30)
print(result)
Output
4530
Work through it line by line
  • The three parameters receive the values 1, 15 and 30.
  • The function calculates using local data.
  • return total sends 4530 back to the call.
  • The main program stores the returned value in result.

Another example

A function can use selection before deciding which value to return.

lesson-17-example-2.py
def larger(a, b):
    if a >= b:
        return a
    else:
        return b

print(larger(8, 5))
Output
8
Why this example matters
  • The condition decides which result is returned.
  • Once a return statement is executed, that function call ends.

Common mistakes

Avoid these errors
  • Printing a result inside a function when the problem specifically requires the value to be returned for later use.
  • Calling a function but ignoring its returned value when it is needed later.
  • Confusing parameters in the definition with arguments supplied in a call.
Exam-safe warningOCR notes that returned function values should be stored if they are needed later. In Python, result = function(...) is the same pattern.

Exam-ready checklist

You should now be able to
  • Define and call a function.
  • Use parameters and return correctly.
  • Store or use a returned value.
  • Choose appropriately between a function and a procedure.
  • Translate OCR function ... endfunction to Python def.

What you have learned

  • A function is a subprogram that returns a value.
  • Use parameters to receive input values.
  • Use return to send a result back.
  • The returned value can be stored or used in another expression.