4.1.1.10–4.1.1.12 - Subroutines, parameters and return values (Python)

4.1.1.10–4.1.1.12 - Subroutines, parameters and return values (Python)

Learn how named subroutines divide a program into understandable tasks, how parameters form an interface, and how a returned value lets the caller continue its work.

Procedures and functions

A subroutine lets a program give a useful operation a name and call that operation wherever it is needed.

Subroutine

A named, out-of-line block of code that executes when it is called. After it finishes, control normally returns to the statement after the call.

In Python, def defines a function. Executing the definition makes the function available; it does not execute its body. A call such as show_heading() executes the body. Indentation identifies which statements belong to it.

Python
def show_heading():
    print("Activity report")

print("Start")
show_heading()
print("End")

The output is Start, then Activity report, then End, on separate lines. The call transfers control to the named block and the caller resumes afterwards. A procedure performs an action without supplying a useful result to the caller; a function supplies a result. Python uses def for both. A Python function without an explicit return returns None, even if it prints something.

Subroutines reduce repeated code: change one shared definition rather than several copied versions. Their names expose a program's structure; validate_score() tells a reader more than a long unexplained condition. Each small operation can be tested separately, reused, and assigned to a different programmer if its interface is agreed. These advantages depend on sensible boundaries: a function that reads input, changes unrelated data and prints several reports is harder to reuse than one focused operation.

Interfaces and parameters

A caller needs to know what data a subroutine accepts, what result it supplies, and any conditions under which it works.

Subroutine interface

The agreed way to use a subroutine: its name, parameters, expected input meanings/types, returned result and relevant preconditions or effects.

In def total_price(quantity, unit_price):, quantity and unit_price are formal parameters. In total_price(3, 200), the values 3 and 200 are the arguments supplied by the caller. Positional arguments match parameters in order; interchanging quantity and price may produce a plausible but unintended calculation. An interface should describe units as well as types: the price below is integer pence, not pounds. Its precondition is a non-negative integer quantity and a non-negative integer unit price.

Predict a parameterised price

{"code":"def total_price(quantity, unit_price):\n return quantity * unit_price\n\nquantity = int(input())\nunit_price = int(input())\nprint(total_price(quantity, unit_price))\n","instructions":"Predict the output for inputs 3 then 200, and run it. Modify total_price so quantities of at least 5 receive a 10% discount: return quantity * unit_price * 9 // 10 in that branch. Keep the original calculation for smaller quantities. Run inputs 3 then 200 and 5 then 200. Prices are integer pence; rounding down is intentional.","files":[],"original_filename":"main.py","runtime_profile":"python-console-v1","file_mode":"fresh"}

Continue this lesson.

You’re previewing NovaLearn without an account. Create a free account to read the rest.

Create free account