Source: https://senecalearning.com/en-gb/revision-notes/gcse/coding/national-curriculum/3-1-1-subroutines

[Revision notes](https://senecalearning.com/en-gb/revision-notes.md) › [GCSE](https://senecalearning.com/en-gb/revision-notes/gcse.md) › [Coding](https://senecalearning.com/en-gb/revision-notes/gcse/coding.md) › [National Curriculum](https://senecalearning.com/en-gb/revision-notes/gcse/coding/national-curriculum.md) › Subroutines

# Subroutines

Coding: Introduction to Python v3, topic 3.1.1

## Subroutines

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

### Advantages of subroutines

![Advantages of subroutines](https://image-v2.cdn.app.senecalearning.com/2018-07/95d17dd9-261f-4238-9065-f411ea8c7210/code-Data-Computer-Wifi-Internet.jpg)

- 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 ensure it works.

### Parameters and arguments

![Parameters and arguments](https://image-v2.cdn.app.senecalearning.com/2018-07/c45f4db9-a761-4a23-9c16-be48190066bc/2.2.4%20Parameters%20vs%20arguments.png)

- 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](https://image-v2.cdn.app.senecalearning.com/2018-07/44bd33c3-2cd5-453e-9359-41ccb52ffc84/2.2.4%20Types%20of%20subroutine.png)

- 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.

### Local scope

![Local scope](https://image-v2.cdn.app.senecalearning.com/2018-07/8b8802c8-d4ac-4692-a407-e4ca1a5aa044/IP-Address-Code-Computer-Monitor-Screen-.jpg)

- A variable defined within a subroutine will have local scope.
- This means that they can only be accessed within that subroutine.

### Global scope

![Global scope](https://image-v2.cdn.app.senecalearning.com/2018-07/423dbff4-848f-43f6-934b-ce8dde8c95c9/Internet-World-Computer-Networks-Wireless-.jpg)

- 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.

## What Should be in a Subroutine?

Subroutines are key to maintainable code, but must be used correctly.

### Good ideas for subroutines

![Good ideas for subroutines](https://image-v2.cdn.app.senecalearning.com/courseImages/physics/1.5.2%20-%20Energy%20resources/recycle-29227_640-min.jpg)

- Subroutines should include single processes only.
- Subroutines should ideally be designed so that they can be reused in multiple parts of code, both within and outside of your program.
- Good examples of subroutines include:
 - Calculating the sum of a list.
 - Finding a largest element of a list.
 - Opening a specific part of an application.

### Bad ideas for subroutines

![Bad ideas for subroutines](https://image-v2.cdn.app.senecalearning.com/2018-09/11f1567f-060b-4155-952f-3cc14b8b3a66/large-pile.jpg)

- Subroutines should not be used for very long processes
- Subroutines should be split into multiple subroutines so that they don’t often exceed about 30 lines of code.
- Bad examples of subroutines include:
 - The whole application.
 - A very specific, and non-reusable piece of code.

## Test yourself on Subroutines

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

def name(x, y):&emsp;&emsp;
return x + y&emsp;&emsp;
- What is an argument of a subroutine?
- What is global scope?
- Which of the following are good examples of subroutines?

[Practise this topic for free](https://app.senecalearning.com/classroom/course/72d0364c-db0a-4630-8e0a-61f48cae0252/section/acfc2d66-1bf9-4980-907d-ee9747ccc021/session)

- Previous topic: [2.2.1 File Handling](https://senecalearning.com/en-gb/revision-notes/gcse/coding/national-curriculum/2-2-1-file-handling.md)
- Next topic: [3.2.1 Data Flow](https://senecalearning.com/en-gb/revision-notes/gcse/coding/national-curriculum/3-2-1-data-flow.md)

## Coding: Introduction to Python v3 contents

### 1 Introduction to Python
- 1.1 Python
  - [1.1.1 Python](https://senecalearning.com/en-gb/revision-notes/gcse/coding/national-curriculum/1-1-1-python.md)
- 1.2 Input & Output
  - [1.2.1 Input & Output](https://senecalearning.com/en-gb/revision-notes/gcse/coding/national-curriculum/1-2-1-input-and-output.md)
- 1.3 Data Types
  - [1.3.1 Data Types](https://senecalearning.com/en-gb/revision-notes/gcse/coding/national-curriculum/1-3-1-data-types.md)
- 1.4 Variables & Constants
  - [1.4.1 Variables & Constants](https://senecalearning.com/en-gb/revision-notes/gcse/coding/national-curriculum/1-4-1-variables-and-constants.md)
- 1.5 Operators
  - [1.5.1 Arithmetic & Relational Operators](https://senecalearning.com/en-gb/revision-notes/gcse/coding/national-curriculum/1-5-1-arithmetic-and-relational-operators.md)
  - [1.5.2 Boolean & Assignment Operators 2](https://senecalearning.com/en-gb/revision-notes/gcse/coding/national-curriculum/1-5-2-boolean-and-assignment-operators-2.md)
- 1.6 Program Flow
  - [1.6.1 Selection](https://senecalearning.com/en-gb/revision-notes/gcse/coding/national-curriculum/1-6-1-selection.md)
  - [1.6.2 Iteration](https://senecalearning.com/en-gb/revision-notes/gcse/coding/national-curriculum/1-6-2-iteration.md)

### 2 Data Structures
- 2.1 Lists & Strings
  - [2.1.1 Lists](https://senecalearning.com/en-gb/revision-notes/gcse/coding/national-curriculum/2-1-1-lists.md)
  - [2.1.2 Strings](https://senecalearning.com/en-gb/revision-notes/gcse/coding/national-curriculum/2-1-2-strings.md)
- 2.2 File Handling
  - [2.2.1 File Handling](https://senecalearning.com/en-gb/revision-notes/gcse/coding/national-curriculum/2-2-1-file-handling.md)

### 3 Modularity
- 3.1 Subroutines
  - [3.1.1 Subroutines](https://senecalearning.com/en-gb/revision-notes/gcse/coding/national-curriculum/3-1-1-subroutines.md)
- 3.2 Data Flow
  - [3.2.1 Data Flow](https://senecalearning.com/en-gb/revision-notes/gcse/coding/national-curriculum/3-2-1-data-flow.md)

### 4 Good Practice
- 4.1 Naming & Whitespace
  - [4.1.1 Identifiers](https://senecalearning.com/en-gb/revision-notes/gcse/coding/national-curriculum/4-1-1-identifiers.md)
  - [4.1.2 Whitespace](https://senecalearning.com/en-gb/revision-notes/gcse/coding/national-curriculum/4-1-2-whitespace.md)
- 4.2 Comments
  - [4.2.1 Comments](https://senecalearning.com/en-gb/revision-notes/gcse/coding/national-curriculum/4-2-1-comments.md)
- 4.3 Exceptions
  - [4.3.1 Exceptions](https://senecalearning.com/en-gb/revision-notes/gcse/coding/national-curriculum/4-3-1-exceptions.md)
