3.1.1
Subroutines
Subroutines
Subroutines
A subroutine is a named block of code within your program.
![Illustrative background for Advantages of subroutines](https://image-v2.cdn.app.senecalearning.com/2018-07/95d17dd9-261f-4238-9065-f411ea8c7210/code-Data-Computer-Wifi-Internet,h_400,q_80,w_640.jpg)
![Illustrative background for Advantages of subroutines ?? "content](https://image-v2.cdn.app.senecalearning.com/2018-07/95d17dd9-261f-4238-9065-f411ea8c7210/code-Data-Computer-Wifi-Internet,h_400,q_80,w_640.jpg)
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 ensure it works.
![Illustrative background for Parameters and arguments](https://image-v2.cdn.app.senecalearning.com/2018-07/c45f4db9-a761-4a23-9c16-be48190066bc/2.2.4%20Parameters%20vs%20arguments,h_400,q_80,w_640.png)
![Illustrative background for Parameters and arguments ?? "content](https://image-v2.cdn.app.senecalearning.com/2018-07/c45f4db9-a761-4a23-9c16-be48190066bc/2.2.4%20Parameters%20vs%20arguments,h_400,q_80,w_640.png)
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.
![Illustrative background for Types of subroutine](https://image-v2.cdn.app.senecalearning.com/2018-07/44bd33c3-2cd5-453e-9359-41ccb52ffc84/2.2.4%20Types%20of%20subroutine,h_400,q_80,w_640.png)
![Illustrative background for Types of subroutine ?? "content](https://image-v2.cdn.app.senecalearning.com/2018-07/44bd33c3-2cd5-453e-9359-41ccb52ffc84/2.2.4%20Types%20of%20subroutine,h_400,q_80,w_640.png)
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.
Scope
Scope
The scope of a variable is the group of areas of the code from which it can be accessed.
![Illustrative background for Local scope](https://image-v2.cdn.app.senecalearning.com/2018-07/8b8802c8-d4ac-4692-a407-e4ca1a5aa044/IP-Address-Code-Computer-Monitor-Screen-,h_400,q_80,w_640.jpg)
![Illustrative background for Local scope ?? "content](https://image-v2.cdn.app.senecalearning.com/2018-07/8b8802c8-d4ac-4692-a407-e4ca1a5aa044/IP-Address-Code-Computer-Monitor-Screen-,h_400,q_80,w_640.jpg)
Local scope
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 scope](https://image-v2.cdn.app.senecalearning.com/2018-07/423dbff4-848f-43f6-934b-ce8dde8c95c9/Internet-World-Computer-Networks-Wireless-,h_400,q_80,w_640.jpg)
![Illustrative background for Global scope ?? "content](https://image-v2.cdn.app.senecalearning.com/2018-07/423dbff4-848f-43f6-934b-ce8dde8c95c9/Internet-World-Computer-Networks-Wireless-,h_400,q_80,w_640.jpg)
Global scope
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.
What Should be in a Subroutine?
What Should be in a Subroutine?
Subroutines are key to maintainable code, but must be used correctly.
![Illustrative background for Good ideas for subroutines](https://image-v2.cdn.app.senecalearning.com/courseImages/physics/1.5.2 - Energy resources/recycle-29227_640-min,h_400,q_80,w_640.jpg)
![Illustrative background for Good ideas for subroutines ?? "content](https://image-v2.cdn.app.senecalearning.com/courseImages/physics/1.5.2 - Energy resources/recycle-29227_640-min,h_400,q_80,w_640.jpg)
Good ideas for subroutines
Good ideas for subroutines
- 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.
![Illustrative background for Bad ideas for subroutines](https://image-v2.cdn.app.senecalearning.com/2018-09/11f1567f-060b-4155-952f-3cc14b8b3a66/large-pile,h_400,q_80,w_640.jpg)
![Illustrative background for Bad ideas for subroutines ?? "content](https://image-v2.cdn.app.senecalearning.com/2018-09/11f1567f-060b-4155-952f-3cc14b8b3a66/large-pile,h_400,q_80,w_640.jpg)
Bad ideas for subroutines
Bad ideas for subroutines
- 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.
1Introduction to Python
1.1Python
1.2Input & Output
1.3Data Types
1.4Variables & Constants
1.6Program Flow
2Data Structures
2.1Lists & Strings
2.2File Handling
3Modularity
3.1Subroutines
3.2Data Flow
4Good Practice
4.1Naming & Whitespace
4.2Comments
4.3Exceptions
Jump to other topics
1Introduction to Python
1.1Python
1.2Input & Output
1.3Data Types
1.4Variables & Constants
1.6Program Flow
2Data Structures
2.1Lists & Strings
2.2File Handling
3Modularity
3.1Subroutines
3.2Data Flow
4Good Practice
4.1Naming & Whitespace
4.2Comments
4.3Exceptions
![Go student ad image](/en-GB/revision-notes/_next/image?url=%2Fen-GB%2Frevision-notes%2Fimages%2Fgo-student-uk-ad.jpg&w=640&q=100)
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