7.1.2

Modularity

Test yourself

Subroutines

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

Illustrative background for Advantages of subroutinesIllustrative background for Advantages of subroutines ?? "content

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.
Illustrative background for Parameters and argumentsIllustrative background for Parameters and arguments ?? "content

Parameters and arguments

  • Parameters are special variables used to pass values into a subroutine.
  • Arguments are the actual values that are passed in.
Illustrative background for Types of subroutineIllustrative background for Types of subroutine ?? "content

Types of subroutine

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

Scope

The scope of a variable is the group of areas of the code from which it can be accessed.

Illustrative background for Local scopeIllustrative background for Local scope ?? "content

Local scope

  • A variable defined within a subroutine will have local scope.
  • This means that they can only be accessed within that subroutine.
Illustrative background for Global scopeIllustrative background for Global scope ?? "content

Global scope

  • A variable can be made global by adding the 'global' keyword in front of it when it is first used.
  • Global variables may be accessed by any part of the whole program.

Jump to other topics

1Components of a Computer

2Software & Software Development

3Exchanging Data

4Data Types, Data Structures & Algorithms

5Legal, Moral, Cultural & Ethical Issues

6Elements of Computational Thinking

6.1Thinking Abstractly

6.2Thinking Procedurally

6.3Thinking Logically

7Problem Solving & Programming

8Algorithms

Go student ad image

Unlock your full potential with GoStudent tutoring

  • Affordable 1:1 tutoring from the comfort of your home

  • Tutors are matched to your specific learning needs

  • 30+ school subjects covered

Book a free trial lesson