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.
def double(number):
return number * 2
answer = double(6)
print(answer)- 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.
def maximum(a, b):
if a > b:
return a
else:
return b
print(maximum(9, 4))- Trace which branch runs.
- Why does
returnmake this a function rather than a procedure-style subprogram? - What would
maximum(3, 8)return?
Returning a value
def rectangle_area(length, width):
area = length * width
return area
result = rectangle_area(8, 5)
print(result)40
- Arguments 8 and 5 are passed into the parameters.
- The function calculates a local value.
returnsends the result back to the calling code.
Choosing a procedure or a function
| Use | Best choice | Example |
|---|---|---|
| Carry out an action when the caller does not need a result back | Procedure-style subprogram | Display a menu or print a message |
| Calculate or obtain a value that the caller needs to use | Function | Calculate an average or return a maximum value |
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 Exam Reference Language ↔ Python
| OCR Exam Reference Language / term | Python | What to remember |
|---|---|---|
function squared(number) ... return squared endfunction | def squared(number): ... return result | Function 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. |
Worked example
A function can hide a calculation behind a meaningful name.
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)4530
- The three parameters receive the values 1, 15 and 30.
- The function calculates using local data.
return totalsends 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.
def larger(a, b):
if a >= b:
return a
else:
return b
print(larger(8, 5))8
- The condition decides which result is returned.
- Once a
returnstatement is executed, that function call ends.
Common mistakes
- 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.
result = function(...) is the same pattern.Exam-ready checklist
- Define and call a function.
- Use parameters and
returncorrectly. - Store or use a returned value.
- Choose appropriately between a function and a procedure.
- Translate OCR
function ... endfunctionto Pythondef.
What you have learned
- A function is a subprogram that returns a value.
- Use parameters to receive input values.
- Use
returnto send a result back. - The returned value can be stored or used in another expression.