3.2.1
Data Flow
Passing Parameters
Passing Parameters
Passing parameters allows us to send data into a subroutine.
![Illustrative background for Passing parameters](https://image-v2.cdn.app.senecalearning.com/2018-09/0013c8a8-e85c-4acd-a8de-79713db7ecab/post-403145_1280,h_400,q_80,w_640.png)
![Illustrative background for Passing parameters ?? "content](https://image-v2.cdn.app.senecalearning.com/2018-09/0013c8a8-e85c-4acd-a8de-79713db7ecab/post-403145_1280,h_400,q_80,w_640.png)
Passing parameters
Passing parameters
- Passing parameters is the process of passing a piece of data into a subroutine.
- The data is then assigned into a local variable (parameter) inside of the subroutine.
- We can then use this data inside the subroutine.
![Illustrative background for Parameters vs global variables](https://image-v2.cdn.app.senecalearning.com/2018-08/eeeae8d9-efee-41bc-8e5e-23daff64465b/earth-1617121_1920,h_400,q_80,w_640.jpg)
![Illustrative background for Parameters vs global variables ?? "content](https://image-v2.cdn.app.senecalearning.com/2018-08/eeeae8d9-efee-41bc-8e5e-23daff64465b/earth-1617121_1920,h_400,q_80,w_640.jpg)
Parameters vs global variables
Parameters vs global variables
- We could also pass data by putting it inside of a global variable.
- This is considered bad practice, since it can be accessed by any subroutine. Passing parameters is a much more secure, and maintainable approach.
Returning Multiple Values
Returning Multiple Values
Sometimes a situation might arise where we wish to return multiple pieces of data from the same subroutine.
![Illustrative background for Returning values](https://image-v2.cdn.app.senecalearning.com/2018-07/227b3f2c-0edd-4074-9406-bfa10b6e5ef2/Email-Internet-Message-Computer-,h_400,q_80,w_640.jpg)
![Illustrative background for Returning values ?? "content](https://image-v2.cdn.app.senecalearning.com/2018-07/227b3f2c-0edd-4074-9406-bfa10b6e5ef2/Email-Internet-Message-Computer-,h_400,q_80,w_640.jpg)
Returning values
Returning values
- Functions are subroutines that return a value.
- Functions are limited to returning a single value, which is sometimes problematic.
![Illustrative background for Returning a list](https://image-v2.cdn.app.senecalearning.com/2018-09/3fba2487-0107-4ee7-9e33-50edfe5b6f04/checklist-ticklist-tick-list-,h_400,q_80,w_640.jpg)
![Illustrative background for Returning a list ?? "content](https://image-v2.cdn.app.senecalearning.com/2018-09/3fba2487-0107-4ee7-9e33-50edfe5b6f04/checklist-ticklist-tick-list-,h_400,q_80,w_640.jpg)
Returning a list
Returning a list
- One solution to this problem is to use a list of values.
- We can create a list, and append each value we want to return.
- This list can then be returned, containing all of the values.
![Illustrative background for Restructuring subroutines](https://image-v2.cdn.app.senecalearning.com/courseImages/chemistry/2.3.2 Giant Covalent Structures/steel-girder,h_400,q_80,w_640.jpg)
![Illustrative background for Restructuring subroutines ?? "content](https://image-v2.cdn.app.senecalearning.com/courseImages/chemistry/2.3.2 Giant Covalent Structures/steel-girder,h_400,q_80,w_640.jpg)
Restructuring subroutines
Restructuring subroutines
- In other scenarios, it can be a much better idea to restructure subroutines to return only a single value.
- Often, multiple return values is a sign of poor program design.
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