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.
Reviewed by: AI editorial process; not yet individually human-reviewed
Have a quick question? Jump to the Q&A page
Jump to a section
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
- The three basic programming constructs: sequence, selection (if and switch/case) and iteration (count-controlled for loops and condition-controlled while and do until loops), and when to use each.
An OCR J277 2.2.2 answer on the three programming constructs: sequence, selection (if and switch/case) and iteration (count-controlled for loops and condition-controlled while and do until loops), with the OCR Exam Reference Language for each.
- The use of variables and constants, the common data types (integer, real, Boolean, character and string), choosing an appropriate data type, and casting (converting) between data types.
An OCR J277 2.2.1 answer on variables and constants, the common data types (integer, real, Boolean, character, string), choosing an appropriate data type for data, and casting between data types.
- The common operators: arithmetic (add, subtract, multiply, divide, exponent, MOD and DIV), comparison operators, and Boolean operators (AND, OR, NOT), and how they are used in expressions.
An OCR J277 2.2.2 answer on the common operators: arithmetic (including exponent, MOD and DIV), comparison operators, and the Boolean operators AND, OR and NOT, with worked examples of integer division and modulus.
- Using one-dimensional and two-dimensional arrays, the use of records to store structured data, and basic SQL (SELECT, FROM, WHERE) to search records in a database.
An OCR J277 2.2.3 answer on storing structured data: one-dimensional and two-dimensional arrays, records, and using basic SQL (SELECT, FROM, WHERE) to search records in a database table.
- The principles of computational thinking: abstraction, decomposition and algorithmic thinking, and how each is used to analyse a problem and design a solution.
An OCR J277 2.1.1 answer on the principles of computational thinking: abstraction (removing unnecessary detail), decomposition (breaking a problem into smaller parts) and algorithmic thinking (a clear sequence of steps), with examples of how each is applied to a problem.
Sources & how we know this
- OCR GCSE (9-1) Computer Science (J277) specification — OCR (2020)