Skip to content

Subroutines and parameters: GCSE Computer Science revision notes

A subroutine is a named block of code that carries out a specific task. It can be called (run) whenever it is needed.

In short

A subroutine is a named block of code that carries out a specific task. It can be called (run) whenever it is needed. A procedure carries out a task. A function carries out a task and returns a value. (AQA calls both 'subroutines'; Edexcel calls them 'subprograms'.).

For GCSE students in Years 10 and 11, on the AQA, OCR, Edexcel, Eduqas, Edexcel IGCSE and Cambridge IGCSE Computer Science courses.

What subroutines are

  • A subroutine is a named block of code that carries out a specific task. It can be called (run) whenever it is needed.
  • A procedure carries out a task. A function carries out a task and returns a value. (AQA calls both 'subroutines'; Edexcel calls them 'subprograms'.)

Parameters and return values

  • Parameters are the variables in the subroutine's definition that receive data. The actual values passed in when it is called are called arguments.
  • A function area(length, width) that returns length × width gives 12 when called as area(3, 4).

Local and global variables

  • A local variable only exists inside its subroutine. A global variable can be used anywhere in the program.
  • Local variables are good practice: the subroutine is self-contained, and its variable names can't clash with the rest of the program.

Why use subroutines?

  • Code is written once and reused, so programs are shorter. Each subroutine can be tested separately, which makes debugging easier.
  • Programs are easier to read and maintain, and different programmers can work on different subroutines at the same time.
  • A structured approach uses subroutines with clear parameters and return values, and good documentation.

Key terms

Subroutine
A named block of code that carries out a specific task.
Procedure
A subroutine that carries out a task.
Function
A subroutine that returns a value.
Parameter
A variable in a subroutine's definition that receives a value.
Argument
The actual value passed into a subroutine when it is called.
Return value
The value a function sends back to where it was called.
Local variable
A variable that only exists inside its subroutine.
Global variable
A variable that can be used anywhere in the program.

Quick questions on Subroutines and parameters

What is the difference between a function and a procedure?
A function returns a value; a procedure doesn't have to: Both carry out a task.
What is a parameter?
A variable that receives a value passed into a subroutine: For example, length and width in area(length, width).
A function area(length, width) returns length × width. What does area(3, 4) return?
12: 3 × 4 = 12.
What is a local variable?
A variable that only exists inside its subroutine: It disappears when the subroutine ends.
Why is using local variables good practice?
The subroutine is self-contained and names can't clash: Other parts of the program can't change them by accident.
Which of these is a benefit of using subroutines?
Code can be reused instead of repeated: Write it once, call it many times.
What is the value passed into a subroutine when it is called?
An argument: The parameter receives the argument.
Which keyword sends a value back from a function?
RETURN: The value goes back to where the function was called.
What is a global variable?
A variable that can be used anywhere in the program: It is declared outside all the subroutines.
What does splitting a program into subroutines make easier?
Testing each part separately: Each subroutine can be checked on its own.
A function double(n) returns n × 2. What does double(double(3)) return?
12: double(3) = 6, and double(6) = 12.
What is a structured approach to programming?
Building programs from subroutines with clear parameters and return values: It makes programs easier to understand and maintain.
This function doubles a number. What is printed?
9: double(4) returns 8, then 1 is added: 9.
What does this area function print?
15: The arguments 3 and 5 go into the parameters w and h, and the function returns 3 × 5.
A variable is changed inside a function. What is printed?
5: The x inside change() is a local variable: it only exists inside the function. The global x is still 5.
Which line calls the function?
Line 4: Line 1 defines the function. Line 4 calls it with the argument 6 and stores what it returns.
In line 1, what is n?
A parameter: n is a parameter: the name used inside the function. The value 6 passed in on line 4 is the argument.
This function returns the bigger number. What is printed?
16: biggest(3, 9) returns 9 and biggest(7, 2) returns 7: 9 + 7 = 16.
This subroutine prints rather than returns. What does the program print?
Hi Sam, then Hi Jo: Each call runs the subroutine with a different argument, in the order they are called.

Subroutines and parameters: exam-style questions and mark schemes

  1. Which of these is true of a function but not of a procedure? [1 mark]

    Answer: It returns a value

  2. Explain the difference between a local variable and a global variable. [2 marks]

    • A local variable can only be used inside the subroutine where it is declared (1)
    • A global variable can be used anywhere in the program (1)
  3. Give three benefits of using subroutines in a large program. [3 marks]

    • Code can be reused, so it only has to be written once (1)
    • Each subroutine can be tested and debugged separately (1)
    • The program is easier to read and maintain, and different programmers can work on different subroutines (1)
  4. Write a function called isEven that takes an integer as a parameter and returns True if it is even and False if it is odd. [3 marks]

    • A function heading with one parameter (1)
    • Uses MOD 2 to check whether the remainder is 0 (1)
    • Returns True for even numbers and False for odd numbers (1)
  5. Write a function called addVat that takes a price as a parameter and returns the price with 20% VAT added. Then write a line that uses it to output the total for a price of 50. [4 marks]

    • Defines a function called addVat with one parameter (1)
    • Works out the price × 1.2 (or the price + the price × 0.2) (1)
    • Returns the result, rather than only printing it (1)
    • Calls addVat(50) and outputs what it returns, 60 (1)
  6. Explain the benefits of using subroutines in a large program, and the difference between local and global variables. [6 marks]

    • a subroutine is a named block of code that is called when it is needed
    • code isn't repeated, so the program is shorter
    • each subroutine can be tested and debugged on its own
    • a team can work on different subroutines at the same time
    • subroutines can be reused in other programs
    • parameters pass data in, and a function returns a value
    • a local variable only exists inside its subroutine, so names don't clash and memory is freed
    • a global variable can be used anywhere, but can be changed by mistake, which makes bugs harder to find

How to answer them: exam technique and command words.

Subroutines and parameters: questions and answers

What is a global variable?
A variable that can be used anywhere in the program. It is declared outside all the subroutines.
What does splitting a program into subroutines make easier?
Testing each part separately. Each subroutine can be checked on its own.
A function double(n) returns n × 2. What does double(double(3)) return?
double(3) = 6, and double(6) = 12.

Practise Subroutines and parameters: 19 questions

Download these notes as a PDF

Previous topic: Data types, arrays and recordsNext topic: Robust programs and testing

More in Programming

LycoLearn is still loading…

This can take a little longer on a busy school or home network. If nothing happens, try refreshing the page.

LycoLearn didn’t load properly

Part of it didn’t arrive. Try refreshing the page in a minute, or open LycoLearn in another browser.