66 exam-style questions plus unlimited generated ones on this topic, at up to four difficulty levels, with full mark schemes. Notes and past-paper booklets are open to everyone. Practice needs a free account.
Or take a quick quiz: 10 questions on this subtopic in GCSE Computer Science (OCR), marked as you tap.
Sub programs (procedures and functions) are named blocks of code that do one task and can be called from anywhere in a program. You need the difference between a procedure and a function, how parameters and return values work, local and global variables, and why sub programs make code better. Expect definitions and short functions to write.
Key facts
Sub program: named block of code for one task, run by calling its name
Procedure: does a task, returns no value (procedure … endprocedure)
Function: returns a value (function … return … endfunction)
Parameter: variable in the sub program's first line that receives a value passed in
Local variable: exists only inside its sub program
Global variable: can be used anywhere (global in ERL)
Benefits: reuse, shorter code, easier to test and maintain, shared between a team
Notes
Procedures and functions
diagram
A sub program is a named block of code that carries out a specific task. It runs when it is called by its name.
A procedure carries out a task but does not return a value.
A function carries out a task and returns a value to the code that called it.
function area(width, height)
return width * height
endfunction
a = area(3, 4)
Here a becomes 12. The returned value is usually stored, output or used in a calculation.
A function call: values go in through the parameters, and one value comes back
Parameters and variables
Parameters are the variables in brackets in the sub program's first line (width and height). They receive the values passed in when it is called (3 and 4). An array can be passed in as a parameter, and a function can return an array.
A local variable is created inside a sub program. It only exists while the sub program runs and cannot be used outside it.
A global variable is declared in the main program and can be used anywhere in the program. In ERL: global total = 0. Constants can also be local or global in the same way.
Local variables are preferred: the same name can be used in different sub programs without clashing, and a sub program cannot change data elsewhere by mistake.
Why use sub programs
The code is written once and can be called many times, so the program is shorter.
Each sub program can be tested on its own, so errors are easier to find.
A change only needs making in one place, so the program is easier to maintain.
Different programmers can write different sub programs, and sub programs can be reused in other programs.
How to answer each type of question
State the difference between a function and a procedure
2 marksGrade 5
Function: returns a value.
Procedure: does not return a value.
Two sentences, one about each.
Example. State the difference between a function and a procedure. [2]
Show the model answerHide the model answer
A function returns a value to the part of the program that called it (1). A procedure does not return a value (1).
Identify parts of a sub program
3 to 4 marksGrade 6
Parameters are in brackets in the first line.
Local variables are assigned inside the sub program.
Trace the call to find the value returned or output.
Example. Look at the code.
01 function largest(a, b)
02 if a > b then
03 big = a
04 else
05 big = b
06 endif
07 return big
08 endfunction
09 result = largest(7, 12)
10 print(result)
(a) Identify the parameters of the function. [1] (b) Identify a local variable. [1] (c) State the output of the program. [1] (d) Give one benefit of writing this code as a function. [1]Show the model answerHide the model answer
(a) a and b
(1)
(b) big
(1)
(c) 12
(1)
(d) It can be called many times without writing the code again / it can be tested on its own
(1)
Write a function
3 to 4 marksGrade 7
Write the first line with the function name and its parameters, exactly as the question names them.
Do the processing using the parameters (not new input).
Return the result, and end with endfunction.
If asked, show how to call it and use the value returned.
Example. (a) Write a function called addVAT that takes a price as a parameter and returns the price with 20% VAT added. [3] (b) Write a line of code that uses the function to output the price of an item costing £15. [1]
Show the model answerHide the model answer
(a)
function addVAT(price)
total = price * 1.2
return total
endfunction
First line with the name addVAT and the parameter price (1); calculates price × 1.2 (1); returns the result
(1).
(b) print(addVAT(15))
(1)
Don’t lose marks
Saying a procedure 'returns nothing useful' without saying a function returns a value.
Using input() inside a function instead of using the parameters passed in.
Printing the answer inside a function when the question asks for it to be returned.
Calling a function but not storing or using the value it returns.
Using a local variable outside the sub program it belongs to.
More tips
Memory tricks
Function = gives you something back (like a vending machine). Procedure = just does a job (like a light switch).
If you see return, it is a function.
Local = lives inside the sub program; global = visible everywhere.
Exam technique
Use exactly the sub program name and parameter names the question gives.
When asked for benefits, give different ones (reuse, testing, maintenance, teamwork), each with a reason.
'Return' and 'output' are different: return sends a value back to the code; output shows it on screen.
What each grade needs
What you need to be able to do, from the first marks up to the top grade.
Grade 4
Describe what a sub program isA named block of code that does a specific task and can be called when needed.
Grade 5
Tell functions and procedures apartA function returns a value; a procedure does not.
Grade 5
Give benefits of using sub programsCode is reused, easier to test and maintain, and can be shared between programmers.
Grade 6
Identify parameters and return valuesParameters receive values passed in; return sends a value back to where it was called.
Grade 7
Write a function with parametersUse function … endfunction, with parameters and a return statement.
Grade 7
Explain local and global variablesA local variable exists only in its sub program; a global variable can be used anywhere.
Quick recall
Cover the answers and test yourself. The app has these as flashcards that come back just before you'd forget them.
Look at this function.
function square(n)
return n * n
endfunction
Identify the parameter used by the function square.
Write the missing word so that the function sends back double the value of n.
return
State what is meant by a local variable.
A variable declared inside a sub program that can only be used inside that sub program.
Name the two types of sub program.
Function and procedure.
Complete the sentences. Use words from this list: call, global, local, loop, parameter. To run a sub program, the programmer writes a ______ to it.
call
Sample questions
Written for this site in the style of OCR exam questions. They are not taken from real past papers.
Question 1Easy3 marks
Look at this function.
function square(n)
return n * n
endfunction
(a) State one difference between a function and a procedure.
[1]
(b) Identify the parameter used by the function square.
[1]
(c) State the value of square(6) + 1.
[1]
Show the answer and mark scheme
(a)
a function returns a value, but a procedure does not
(b)Answer: n
n
(c)Answer: 37
37
Question 2Medium5 marks
Look at this program.
01 global total = 0
02 procedure addPoints(p)
03 bonus = 5
04 total = total + p + bonus
05 endprocedure
06 addPoints(10)
07 addPoints(3)
08 print(total)
(a) State the output of the program.
[1]
(b) Identify one local variable and one global variable in the program.
[2]
(c) Explain one advantage of using local variables.
[2]
Show the answer and mark scheme
(a)Answer: 23
23
(b)
local: bonus / p
global: total
(c)
a local variable can only be accessed / changed inside its sub program
so it cannot be changed by mistake by other parts of the program / the same name can be used in other sub programs / its memory is freed when the sub program ends
Question 3Hard8 marks
A prime number is a whole number greater than 1 whose only factors are 1 and itself.
(a) Write a function called isPrime that takes a whole number as a parameter and returns True if it is prime and False if it is not. You must use either OCR Exam Reference Language or a high-level programming language that you have studied.
[5]
(b) Write code that uses isPrime to output every prime number from 1 to 50.
[2]
(c) Explain why isPrime is written as a function rather than as a procedure.
[1]
Show the answer and mark scheme
(a)Answer:
function isPrime(n)
if n < 2 then
return false
endif
for d = 2 to n - 1
if n MOD d == 0 then
return false
endif
next d
return true
endfunction
function with one parameter that returns a Boolean
returns False for numbers less than 2
loops through possible factors from 2 up to n − 1 (or up to the square root of n)
uses MOD to test whether n is divisible by each value, returning False if it is
returns True only after every possible factor has been checked
(b)Answer:
for i = 1 to 50
if isPrime(i) then
print(i)
endif
next i
loops through the numbers 1 to 50
calls isPrime for each number and outputs the number when True is returned
(c)
it needs to return a value (True or False) that the code calling it can use