Revise Computingrevisecomputing.co.uk
At a glanceFeaturesStudentsPricingHow it worksFree GCSE notesExam dates
At a glanceFeaturesStudentsPricingHow it worksFree GCSE notesExam dates

Knowledge organisers / Additional Programming Techniques

How to use sub programs (functions and procedures) to produce structured code

All topicsPractise exam questions
Knowledge organiser

Additional Programming Techniques

2.2.3h

The difference and use of functions and procedures and where to use them effectively

What you need to know

Sub-programs (also called sub-routines) are reusable blocks of code that perform a specific task. There are two types: functions return a value, while procedures perform a task without returning a value. Using sub-programs makes code modular, reusable, and easier to test and maintain.

Key points

  • A function performs a task and RETURNS a value using the return keyword.
  • A procedure performs a task but does NOT return a value (it may print output instead).
  • Parameters are values passed into a sub-program when it is called. They MUST be used within the function.
  • Common Mistake:DO NOT overwrite parameters by asking for input inside the function. If direction and position are given as parameters, USE them — don't do direction = input() inside the function.
  • Sub-programs must be CALLED in the main program to execute. Calling: totalPay = calculatePay(experience, miles).
  • Using sub-programs makes code modular, reusable, and easier to maintain.
  • Code does not need to be repeated; the sub-program can be called multiple times.
  • Exam Tip:A function must RETURN its result — not just print it. Use return total, not print(total), inside the function.
  • Exam Tip:When CALLING a function, string parameters need quotation marks: freeseats("Red"), not freeseats(Red). Store the result: redseats = freeseats("Red").
  • Exam Tip:You can define the function THEN call it separately: result = newPrice(5, "premium") then print(result).
  • Exam Example:Refining to use a function means replacing inline code with a function call: totalPay = calculatePay(experience, miles) then print(totalPay).

Code examples

Function vs Procedure
python
# FUNCTION - returns a value
def newPrice(nights, room):
    if room == "basic":
        price = 60 * nights
    else:
        price = 80 * nights
    return price   # RETURN, not print

# CALLING the function:
result = newPrice(5, "premium")
print(result)  # Output: 400

# PROCEDURE - does not return a value
def greet(name):
    print("Hello, " + name + "!")

greet("Alice")  # Output: Hello, Alice!