Skip to main content
EnglandComputer ScienceSyllabus dot point

What are procedures and functions, how do parameters and return values work, and why use subprograms?

The use of subprograms (procedures and functions), passing parameters into a subprogram, returning values from a function, local versus global variable scope, and generating random numbers.

An OCR J277 2.2.3 answer on subprograms: procedures and functions, passing parameters, returning values, the difference between local and global variables, the benefits of subprograms, and generating random numbers.

Generated by Claude Opus 4.811 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this dot point is asking
  2. Procedures and functions
  3. Parameters and return values
  4. Local and global variables
  5. Why use subprograms
  6. Generating random numbers
  7. Try this

What this dot point is asking

OCR wants you to use subprograms, both procedures and functions, pass parameters into them, return values from functions, and understand local versus global variable scope. You should also be able to generate random numbers. Subprograms are a key part of writing structured, reusable code, and are examined in Paper 2, including in Section B programs.

Procedures and functions

Parameters and return values

A function definition and call:

function add(a, b)
  return a + b
endfunction

answer = add(5, 3)
print(answer)

Local and global variables

Why use subprograms

Generating random numbers

Try this

Q1. State the difference between a procedure and a function. [1 mark]

  • Cue. A function returns a value to the calling code; a procedure performs a task but does not return a value.

Q2. State what a parameter is. [1 mark]

  • Cue. A value passed into a subprogram when it is called, so it can work on different data.

Q3. Write a line that generates a random whole number from 1 to 100 and stores it in a variable called target. [1 mark]

  • Cue. target = random(1, 100).

Exam-style practice questions

Practice questions written in the style of OCR exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.

OCR 20214 marksExplain the difference between a procedure and a function, and explain what is meant by a parameter.
Show worked answer →

Procedure (up to 2): a named block of code that performs a task or action but does not return a value to the part of the program that called it (for example a procedure that draws a menu on screen).

Function (up to 2): a named block of code that performs a task and returns a value to the calling code (for example a function that takes two numbers and returns their average).

Parameter (1, within the four marks): a value passed into a subprogram when it is called, which the subprogram uses to do its job, allowing the same subprogram to work on different data each time.

Markers reward the returns-a-value distinction (function returns, procedure does not) and a clear description of a parameter as data passed in. Saying "a function is just a procedure" loses the distinction.

OCR 20225 marksWrite a function called area that takes the width and height of a rectangle as parameters and returns the area. Then write a line that calls the function for a rectangle 4 by 6 and stores the result in a variable. Explain why using a function here is good practice.
Show worked answer →

Function (up to 3):

function area(width, height)
  return width * height
endfunction

Marks: correct function header with two parameters (1), correct calculation width * height (1), correct use of return (1).

Call (1): rectArea = area(4, 6) calls the function with arguments 4 and 6 and stores the returned value (24).

Good practice (1): the calculation is written once and can be reused (called many times) with different values, which avoids repetition, is easier to test, and makes the program clearer and easier to maintain.

Markers reward a correct function that returns a value, a correct call that stores the result, and a genuine benefit of using a subprogram.

Related dot points

Sources & how we know this