3.2.1
Data Flow
Passing Parameters
Passing Parameters
Passing parameters allows us to send data into a subroutine.
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.
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.
Returning values
Returning values
- Functions are subroutines that return a value.
- Functions are limited to returning a single value, which is sometimes problematic.
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.
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
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