OCR GCSE (J277)

An interactive lesson and quiz on Functions and Procedures.

Part 1: Functions vs. Procedures

Subprograms help us write structured, reusable code. There are two types:

Procedure (The "Do-er")

A procedure is a block of code that you call to perform a task. That's it.

Analogy: A "Print" button. You click it, it *does* an action (prints), but it doesn't *give* you a value back.


procedure display_menu():
  print("1. Add")
  print("2. Exit")
endprocedure

display_menu()
                        

Function (The "Giver")

A function is a block of code that performs a task and must return a single value.

Analogy: A calculator's `sqrt(9)` button. You give it '9', it *does* a calculation, and it *gives* you '3' back to use.


function get_choice():
  choice = input()
  return choice
endfunction

user_choice = get_choice()
                        

Part 2: Parameters & Scope

How subprograms send and receive data.

Parameters & Arguments

Subprograms need data to work on. We pass data *into* them.

  • A Parameter is the "placeholder" variable in the subprogram's definition (e.g., `num1`).
  • An Argument is the "actual value" you send in when you call it (e.g., `5`).

# 'num1' is the PARAMETER
function add_five(num1):
  total = num1 + 5
  return total
endfunction

# '10' is the ARGUMENT
result = add_five(10)
print(result) # Outputs 15
                    

Variable Scope (Local vs. Global)

The "Classroom Rule": Variables created in the main program are like notes on the main whiteboard (global), which everyone can see. Variables created inside a subprogram are like scratch work on a student's private mini-whiteboard (local).

  • Local Variable: Created *inside* a subprogram (like `total` above). It only exists while the subprogram is running and is invisible to the rest of your code.
  • Global Variable: Created *outside* any subprogram. It can be seen from anywhere (but it's bad practice to rely on it!).

Part 3: Quiz (Page 1 of 7)

1. What is the main difference between a function and a procedure?
2. Which keyword is the key feature of a function, but is never used in a procedure?
3. A variable created *inside* a subprogram that is only visible *inside* that subprogram is called a:
4. In `def greet(name):`, the `name` variable is known as a...
5. In the call `greet("Alex")`, the value `"Alex"` is known as an...

Part 3: Quiz (Page 2 of 7)

6. Fill in the blank to complete this procedure definition. (Python-style)
def greet():
print("Hello", name)

The "placeholder" variable in a definition is called a...

7. Fill in the blank to call the procedure and pass it the name 'Alex'.
# Call the procedure from Q6 greet()

The "actual value" you pass in is called an... (Note: Don't forget the ' ' for a string!)

8. Fill in the blank to send the `total` back to the main program.
def add(num1, num2):
total = num1 + num2
 total
9. A procedure is defined as `def show_score(points):`. Fill in the blank to call it with the value 100.
10. A function is defined as `def get_initial(name):`. Call it with `"Priya"` and store the returned value in the `initial` variable.

Part 3: Quiz (Page 3 of 7)

11. Drag the tokens to build a function that calculates the area of a rectangle. (Unlimited attempts)

Drag from here:

return area print(area) area = w * h def calc_area(w, h):
12. Drag the tokens to build a procedure that prints a custom error message. (Unlimited attempts)

Drag from here:

return msg def show_error(msg): print('Error:', msg) msg = 'Error'

Part 3: Quiz (Page 4 of 7) - Debugging

13. (Misconception 1) This code is broken. It's meant to be a function, but it just prints the total instead of *returning* it.

def add_nums(a, b):
  total = a + b
  print(total)

result = add_nums(5, 3)
# 'result' will be 'None'
                    

Re-type only the 3-line function definition correctly.

14. (Misconception 2) This code is broken. It ignores its parameter and uses `input()` inside, making it not reusable.

def welcome(name):
  name = input("Enter name:")
  print("Welcome", name)

welcome("Alex") # This "Alex" is ignored!
                    

Re-type the procedure definition correctly so it uses its parameter.

15. This code is broken. The `local_var` is not accessible outside the function. Fix the code so it correctly prints the value.

def get_number():
  local_var = 10

get_number()
print(local_var) # NameError
                    

Re-type all the lines of code, but corrected so it runs without an error. Store the returned value in a variable named `num`.

16. This code is broken. The function is called with the wrong number of arguments. Fix the *call* line.

def calc_area(length, width):
  return length * width

area = calc_area(10) # Missing argument
print(area)
                    

Re-type the last two lines correctly, giving it a width of 5.

Part 3: Quiz (Page 5 of 7) - Scope Challenge

17. Trace the code. What will be printed on each line?

01  def my_function(a):
02    temp = a + 1
03    return temp
04
05  score = 10
06  name = "Dru"
07
08  print(score)
09  print("name")
10  print(my_function(score))
11  print(score)
                    
18. Trace this code. What is the final output?

01  def func_a(x):
02    x = x + 1
03    return x
04
05  def func_b(y):
06    y = y * 2
07    print(y)
08
09  x = 10
10  y = 5
11
12  x = func_a(x)
13  func_b(y)
14  print(x)
                    

Part 3: Quiz (Page 6 of 7)

19. Why is the code from Q14 (shown below) considered bad practice?

def welcome(name):
  name = input("Enter name:")
  print("Welcome", name)
                    
20. One benefit of subprograms is "reusability". What is another key benefit?
21. What is the main benefit of using local variables?
22. What is the main problem with relying on global variables?

Part 4: Final Challenge (Write from Scratch)

Time to write the full subprograms yourself! (Use Python-style syntax: `def`, `return`, etc.)

23. Write a full procedure named `display_total` that takes `total_cost` as a parameter and prints it in a friendly message (e.g., "Total cost: 50").
24. Write a full function named `calc_vat` that takes `price` as a parameter. It should calculate VAT (20% of the price) and return the VAT amount.
25. Write a full function named `calc_diff` that takes two parameters, `a` and `b`, and returns the difference (a - b).
26. Write a full procedure named `count_to` that takes one parameter, `n`. It should print all numbers from 1 up to (and including) `n`. (You must use `i` or `x` as your loop variable.)

Subroutine Exam Walkthroughs

Tackle 3, 4, and 6-mark subroutine questions. These walkthroughs focus on logic error spotting, array iteration, and variable scope.

Tip: Pay close attention to the 6-mark question at 3:45. It explains how to combine loops and arrays—a very common OCR exam topic!
Tip: This video covers Variable Scope and Input Validation—essential for scoring high on Paper 2's debugging questions.

Lesson Complete!

Well done, Student!

This is your 1st attempt.

Your final score:
0 / 24

Your Personal Feedback: