Chhetri AcademyGCSE & A level Paper Builder

Functions and procedures

OCR GCSE Computer Science (J277) · Algorithms and programming (Paper 2) › Programming fundamentals › Additional programming techniques

Practise Functions and procedures. 11 exam-style questions plus unlimited generated ones on this subtopic, at up to four difficulty levels, with full mark schemes and a progress tracker. Free, no account needed.

Build a paper on this topic

▶ Watch videos on Functions and procedures (Craig 'n' Dave OCR on YouTube) · Practise all of Additional programming techniques

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.
n
State the output of this program.
procedure greet(name)
   print("Welcome, " + name)
endprocedure

greet("Leah")
greet("Sam")
Welcome, Leah
Welcome, Sam

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

Related subtopics

Stuck? Get 1-to-1 help. Chhetri Academy tutors GCSE and A level Maths and Science online, with a free 30-minute trial lesson.

Book a free trial