Subroutines

Computer Science: OCR A Level, topic 7.1.4

Subroutines

A subroutine is a named block of code within your program.

Advantages of subroutines

Advantages of subroutines

  • Easier to read:
  • There are fewer long blocks of code to understand.
  • More efficient:
  • Blocks of code can be written once and reused many times.
  • More reliable:
  • Each subroutine can be individually tested to make sure it works.
Parameters and arguments

Parameters and arguments

  • Parameters are special variables used to pass values into a subroutine.
  • Arguments are the actual values that are passed in.
Types of subroutine

Types of subroutine

  • A function is a subroutine which returns a value.
  • A procedure is a subroutine which does not return a value.

Test yourself on Subroutines

  • In the following code, is x a parameter or an argument?

    FUNCTION name(x, y)  
    return x + y  
    ENDFUNCTION     

  • What is an argument of a subroutine?

Practise this topic for free