Procedures & Functions (CIE IGCSE Computer Science)

Revision Note

Test Yourself
Becci Peters

Expertise

Computer Science

Subroutines

Procedures and functions are types of subroutines. They are a sequence of instructions that perform a specific task or set of tasks

  • Subroutines are often used to simplify a program by breaking it into smaller, more manageable parts. They can be used to:
    • Avoid duplicating code and can be reused throughout a program,
    • Improve the readability and maintainability of code,
    • Perform calculations, to retrieve data, or to make decisions based on input.
  • Parameters are values that are passed into a subroutine. They can:
    • Be used to customise the behaviour of a subroutine,
    • Be of different types,
    • Have default values, which are used if no value is passed in,
    • Be passed by value or by reference.
      • When a parameter is passed by value, a copy of the parameter is made and used within the subroutine
      • When a parameter is passed by reference, the original parameter is used within the subroutine, and any changes made to the parameter will be reflected outside of the subroutine
  • Subroutines can have multiple parameters

What's the difference between a procedure and function?

Functions return a value whereas a procedure does not.

Defining and Calling a Subroutine

A subroutine only needs to be defined once to set it up. After this you can call it as many times as needed to use it.

Procedures

  • Procedures are defined using the PROCEDURE keyword in pseudocode, def keyword in Python, public in Java and Sub keyword in Visual Basic
  • Procedures can be called from other parts of the program using their name

Pseudocode example:

PROCEDURE calculate_area(length: INTEGER, width: INTEGER)

  area length * width

  OUTPUT "The area is " + area

END PROCEDURE

To call the procedure you would use the following pseudocode:

calculate_area(5,3)

Python example:

def calculate_area(length, width):

    area = length * width

    print("The area is ", area)

calculate_area(5,3)

Java example:

public class Main {
  static void calculateArea(int length, int width) {
    int area = length * width;
    System.out.println("The area is " + area);
  }

  public static void main(String[] args) {
    calculateArea(3,6);
  }
}

Visual Basic example:

Sub CalculateArea(length As Integer, width As Integer)

    Dim area As Integer = length * width

    Console.WriteLine("The area is " & area)

End Sub

CalculateArea(5, 3)



Functions

  • Functions are defined using the FUNCTION keyword in pseudocode, def keyword in Python, and Function keyword in Visual Basic and Java
  • Functions return a value using the RETURN keyword in pseudocode, return statement in Python, and function name in Visual Basic and Java
  • Functions can be called from other parts of the program using their name, and their return value can be stored in a variable

Pseudocode example:

FUNCTION calculate_area(length: INTEGER, width: INTEGER)

  area length * width

  RETURN area

END PROCEDURE

To output the value returned from the function you would use the following pseudocode:

OUTPUT(calculate_area(5,3))

Python example:

def calculate_area(length, width):

    area = length * width

    return area

print(calculate_area(5,3))

Java example:

public class Main {
  static int calculateArea(int length, int width) {
    int area = length * width;
    return area;
  }

  public static void main(String[] args) {
    System.out.println(calculateArea(3,6));
  }
}

Visual Basic example:

CalculateArea(5, 3)

Sub CalculateArea(length As Integer, width As Integer)

    Dim area As Integer = length * width

    Return area

End Sub

You've read 0 of your 0 free revision notes

Get unlimited access

to absolutely everything:

  • Downloadable PDFs
  • Unlimited Revision Notes
  • Topic Questions
  • Past Papers
  • Model Answers
  • Videos (Maths and Science)

Join the 100,000+ Students that ❤️ Save My Exams

the (exam) results speak for themselves:

Did this page help you?

Becci Peters

Author: Becci Peters

Becci has been a passionate Computing teacher for over 9 years, teaching Computing across the UK helping to engage, interest and develop confidence in the subject at all levels. Working as a Head of Department and then as an educational consultant, Becci has advised schools in England, where her role was to support and coach teachers to improve Computing teaching for all. Becci is also a senior examiner for multiple exam boards covering GCSE & A-level. She has worked as a lecturer at a university, lecturing trainee teachers for Computing.