Edexcel International Advanced Level Computer Science, point 7.1.4
A subprogram is a named block of code that does one job and can be called whenever needed. Using subprograms is a key part of good programming (9.1.3). Check the PLS for the exact features allowed.
c. Function
A function is a subprogram that returns a value to the code that called it.
def area(width, height):
return width * height
result = area(4, 5) # result is 20
print(area(2, 3) + 1) # a function call used in an expression
- Defined with
def, a name, parameters in brackets, and a colon. The body is indented. returnsends the value back and ends the function.- A function is used in an expression or assigned to a variable.
d. Procedure
A procedure is a subprogram that carries out actions but doesn't return a value.
def show_menu():
print("1. Play")
print("2. Quit")
def greet(name):
print("Hello,", name)
show_menu() # called as a statement
greet("Amy")
(In Python both are written with def. A procedure has no return with a value, so it gives back None.)
| Function | Procedure | |
|---|---|---|
| Returns a value | Yes | No |
| Called | In an expression | As a statement |
| Typical use | Calculate something | Do something (display, update) |
a. Parameter and b. Argument
- A parameter is the variable name in the subprogram's definition.
- An argument is the actual value passed in when it is called.
def power(base, exponent): # base and exponent are parameters
return base ** exponent
print(power(2, 5)) # 2 and 5 are arguments -> 32
- Arguments are matched to parameters by position, or by name (keyword arguments):
power(exponent=5, base=2). - Parameters are local variables: they exist only while the subprogram runs.
Default parameters
A parameter can have a default value, used if no argument is given:
def discount(price, percent=10):
return price * (100 - percent) / 100
print(discount(80)) # 72.0
print(discount(80, 25)) # 60.0
Returning more than one value
def min_max(numbers):
return min(numbers), max(numbers)
low, high = min_max([4, 9, 2]) # low = 2, high = 9
Passing by value and reference
Python passes a reference to the object. Changing a list inside a subprogram changes the caller's list, but reassigning a number doesn't:
def add_item(items):
items.append(99) # changes the caller's list
def double(n):
n = n * 2 # only changes the local n
return n
Why use subprograms?
- Reuse: write once, call many times.
- Readability: break a program into named steps.
- Easier testing: test each subprogram separately.
- Easier maintenance: fix in one place.
- Teamwork: different people write different subprograms.
- Abstraction: hide the detail behind a name (10.1.1).
Worked example
Task: write a function is_prime(n) and a procedure show_primes(limit) that prints the primes up to limit.
def is_prime(n):
if n < 2:
return False
for i in range(2, int(n ** 0.5) + 1):
if n % i == 0:
return False
return True
def show_primes(limit):
for n in range(2, limit + 1):
if is_prime(n):
print(n)
show_primes(20) # 2 3 5 7 11 13 17 19
is_primeis a function: it returnsTrueorFalse, and is used in theifcondition.show_primesis a procedure: it prints and returns nothing.nandlimitare parameters, and20is an argument.- Each subprogram has one job, so each can be tested separately (
is_prime(7)should giveTrue).
Exam tip: parameter is in the definition, argument is in the call. A function returns a value, so check you've used return. Don't print inside a function when you should return, unless asked. Give subprograms clear names that say what they do.
Check yourself
What is the difference between a function and a procedure?
A function returns a value to the caller. A procedure carries out actions and doesn't return a value.
What does a Python procedure give back when it is called?
None, because it has no return statement with a value.
What does a default parameter do?
It gives the parameter a value to use if the caller doesn't supply an argument.
Give two benefits of using subprograms
Any two of: reuse; readability; easier testing; easier maintenance; teamwork; abstraction.