3.2.1

Data Flow

Test yourself

Passing Parameters

Passing parameters allows us to send data into a subroutine.

Illustrative background for Passing parametersIllustrative background for Passing parameters ?? "content

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 variablesIllustrative background for Parameters vs global variables ?? "content

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

Sometimes a situation might arise where we wish to return multiple pieces of data from the same subroutine.

Illustrative background for Returning valuesIllustrative background for Returning values ?? "content

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 listIllustrative background for Returning a list ?? "content

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 subroutinesIllustrative background for Restructuring subroutines ?? "content

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.

Jump to other topics

1Introduction to Python

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

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