Parameter Passing

Course Content

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:

Why use subroutines?

Structure of a subroutine

A subroutine is in two parts:

Structure of a Function

What does a function look like?

A function is in two parts:

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.

This is not a good idea….

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

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.

Parameter passing Tutorial Video

1 - Parameter Passing.mp4