Parameter Passing
Course Content
Describe and create procedures/subroutines/subprograms
Create and describe user-defined functions
Describe the benefits of developing programs modularly
Describe what is meant by parameter passing
Describe the difference between calling by reference/value
Describe the difference between local and global variables in terms of scope and memory
What is modularity?
Software is usually written using two types of sub-programs. Using these is an approach known as modularity.
There are two main types of subprograms:
Procedures/Subroutines
Functions
Why use subroutines?
They increase maintainability of code:
Individual subroutines or functions are easily identified and tested/de-bugged and edited.
Improving readability
Individual subprograms can be added/removed easily
Code can be reused
Structure of a subroutine
A subroutine is in two parts:
There is the subroutine declaration itself. Any pieces of data that are needed are passed as parameters.
The subroutine or function is then called and supplied with any parameters necessary.
Structure of a Function
What does a function look like?
A function is in two parts:
There is the function declaration itself. Any pieces of data that are needed are passed as parameters.
An explanation is shown below:
Calling a subroutine
You have to call the function/procedure and pass any parameters/arguments (variables) that the procedure needs from the main program.
So for the previous example:
It is important to notice that the formal and actual parameters do not need to be called the same name. Inside the function the actual parameters are referred to by the names listed in the formal parameters (on the definition line).
Formal vs Actual Parameters
Formal parameters are the identifiers used to stand for a value that will be passed when a subprogram/method is called:
def CelsiusToFahr(celsius):
Actual parameters are the actual values that is passed into the method/subroutine/function.
Fahrenheit = CelsiusToFahr(78)
or
Fahrenheit = CelsiusToFahr(inputtemp)
Global and Local variables
A global variable means it is accessible in EVERY sub-routine of the entire program.
Python assumes each variable is local if is specified inside a subroutine.
Python assumes each variable is global if it is specified outside of a subroutine.
This is not a good idea….
This means EVERY subroutine can change them accidentally.
The way to counter this is to use local variables.
Parameter Passing - Example 1
You will see in this example the local variables called var1 and var2 have the values of 10/20. The global variables var1 and var2 400/600
Example 1 - Local and Global Variables
Inside subProgram1 var1 and var2 are local variables and they only exist within this subroutine - that is why when we print their values they pick up the values 10 and 20.
var1 and var2 are global variables as they are declared outside of any subroutine. So when we reference them in subProgram2 these are the values (400 and 600) that are used.
Example 2 - No Global Variables
Explanation
As we have removed the global variables declarations when we try to reference this value we get the message below saying that the variable var1 is not declared. This is because var1 is a local variable and its scope is local - it only exists in the subroutine in which it was declared (subProgram1).
Parameter Passing - Example 3
Var1 and var2 are declared as local variables in the main routine and then passed as parameters to subProgram2. This allows the values to then be accessed within the subroutine.
Example 4 - Updating Parameters Problem
We are updating the values in this example but these values not passed out so when we reference them outside of this routine these changes are lost
Example 5 - Updating Parameters Fixed
In the example below we have modified our program so that inside the subProgram2 we are returning the values. This means that when we reference them outside of the subProgram2 the changes have been passed out.
Local Variables
Local variables only exist in the subroutine in which they are declared. This is known as the scope of a variable.
For example if there was a variable called counter that was used to control loops and your program had numerous loops then that variables value would change all the time.
Example
The variable counter is present in both functions, but it only exists in the function in which it was declared.
Advantages of Local variables
Aids modularity
Allows subroutines to be used with their own variables
More efficient in terms of memory
Local variables are disposed of when the subroutine finishes
Removes naming clashes with other local variables in other subroutines
E.g loop counter variables
Functions and Subroutines
Although Python does not specify between functions and subroutines/procedures there can be a difference between these in some languages.
A function returns a value(s) which can be assigned to a variable. Where as a subroutine may not pass out any values.