A-level Computer Science revision · Programming

Functions, procedures and scope

The idea

Subroutines are named blocks of code. A function returns a value; a procedure does not. Parameters pass data in, by value (a copy) or by reference (the original). Local variables exist only inside their subroutine; global variables are accessible everywhere, but make code harder to test.

  1. Function: returns a value. Procedure: no return.
  2. By value: changes do not affect the caller. By reference: they do.
  3. Prefer local variables to reduce side effects.

Watch out Changing a parameter passed by value inside a subroutine does not change the original variable.

See it worked

Worked example

Why are local variables preferred to global ones?

  1. They exist only within their subroutine.
  2. Other code cannot change them by accident.
  3. Subroutines are easier to test and reuse.

Answer They avoid unintended side effects and make subroutines reusable

Your turn

Question 11 mark

What is the difference between a function and a procedure?

Choose one
Question 21 mark

A parameter passed by reference is changed inside a subroutine. What happens to the original variable?

Choose one
Question 32 marks

Give one advantage of using parameters instead of global variables.

Write your answer, then see a model answer
  1. Name the benefit.
  2. Explain it.

Answer Parameters make clear what data a subroutine uses, so it can be reused and tested independently without unexpected changes elsewhere.

A-level Computer Science Pro, OCR H446

A-level Computer Science Pro

£119 once

Price rises to £199 on 1 January. Save £80 by joining before then.

At least 12 months of access

By paying you agree to our Terms and get access straight away, which ends your 14-day cancellation right.

No VAT is charged.

Independent practice for OCR A-level Computer Science (H446), not endorsed by OCR.

Privacy · Terms