Slides badge

Subprograms

Learning Intentions

  • Understand the scope of variables

  • Understand the terms 'modular' and 'subprogram'

  • Understand the terms parameter and parameter passing

Success Criteria

  • Be able to explain scope of variables and use local and global variables in Python
  • Be able to explain why subprograms are used and write code that makes use of them, including the use of parameters
  • Be able to distinguish between formal and actual parameters

Subprograms

  • Program code often gets very messy unless it is broken up. A program can be broken into chunks to make the program more modular.

  • Programs are often made up of chunks of code that performs different tasks that are repeated throughout the program.

  • These chunks of code are called subprograms.

  • Each subprogram is given a unique name so that it can be referenced later on without having to rewrite the code each time it is used.

Subprograms

  • Often called subroutines, procedures, methods and functions, a subprogram is a clear and independent piece of code that performs a certain task.

  • We have been looking at predefined functions which are in fact themselves subprograms built in to the programming language.

  • These predefined functions, such as round will perform a specific job and nothing more.

  • We too can define our own subprograms.

Subprograms

  • When we need to run the code within a subprogram, we call it.

  • The subprogram receives data from the program and performs an action on it (for example, you might expect the printDoubleNumber subprogram to print a number multiplied by 2)

  • To call it, we use its name followed by brackets and any parameters we need to pass to it, take for example a subprogram called doubleNumber, we could call it using the parameter 2 by using:

printDoubleNumber(2)

Subprograms

def printDoubleNumber(num):
print(num * 2)


printDoubleNumber(2)

A subprogram definition

A call to a subprogram

Return values and functions

  • A function is like a mathematical function, it takes in a value and then performs a process on it and gives it back as an output.

  • In general, a function is a subprogram that can give back a value. It uses the return keyword to instruct the program to send the data back:

def printDoubleNumber(num):
return num * 2

x = 10
y = printDoubleNumber(x)

print(y)

Parameters

x = 10
printSquareNumber(x)
  • A parameter is a value that is passed into a subprogram when the subprogram is called. These parameters are called actual parameters.
  • In Python a parameter is put within the brackets after the name of the subprogram.
 
 
 
 
 
  • What is the parameter?
  • What is the subprogram name?

 

Parameters

def printDoubleNumber(num):
print(num * 2)
  • Parameters are also provided in subprogram definitions too, for example, in the printDoubleNumber function our subprogram expects one parameter, num.
  • When they are added to the definition of a subprogram they are called formal parameters.

Order of parameters is important

  • The order of your parameters is very important. For example, if you expect the first formal parameter to be a number and the first value given as an actual parameter is a string, the program will crash or at least not run as expected.
def printAverage(name, mark1, mark2):
average = (mark1 + mark2) / 2
print("Pupil", name, " got an average of", average)


printAverage(21, 34, "John")

Identify which of the following programs have formal parameters and which have actual parameters:

Task

def squareIt(num):
def displayOnScreen(num):

1.

2.

print(y)

3.

print(x * 2)

4.

Passing parameters

def addNumber(n):
print(x + n)

def setX():
x = 10

setX()
addNumber(5)
  • When we create a larger program with many subprograms, we need to pass data between these programs by passing it as parameters.
  • Variables declared within one subprogram cannot be accessed by another subprogram unless they are passed as parameters.
  • Parameters can be passed in two ways: by reference or by value

Passing parameters is like an assembly line

  • Think of parameter passing like a factory or assembly line which manufactures cars:
    • One robot (a subprogram) does one task, for example one robot is responsible for what happens under the hood
    • The next robot is responsible for the door assembly in the car but requires a car with everything working under the bonnet (this is a parameter that has been passed). 

Parameter passing by reference

  • Passing by reference sends across the memory location to the subprogram, allowing the subprogram to edit the actual value or variable in memory. 
  • This means that any changes made to the value or variable, will affect the original value or variable.
  • Python DOES NOT support explicit by reference parameter passing, but some data types, such as arrays and dictionaries, automatically are sent by reference.

Parameter passing by reference

  • In other languages, such as PHP or C parameter passing by reference is easy:
function changeX(&$x){
$x = 20;
}

$x = 10;
echo $x;

changeX($x);
echo $x;

Parameter passing by Value

  • In most languages, passing a parameter by value is the default option. 
  • Passing by value makes a copy of the original value or variable and passes it to the subprogram.
  • Any changes to the value or variable will not affect the original value.
  • The main disadvantage is that this uses CPU and memory time to make and store a second copy of that value or variable.

Local and global variables

  • There are two types of variables in a program, local variables and global variables. This is called the scope of the variable.
  • The scope of a global variable is the entire program.
  • The scope of a local variable is the sub-program where it has been declared.
  • A local variable is defined within a single subprogram and is only accessible to that subprogram. 
  • When this subprogram is run, the variable is added into memory.
  • When the execution of the subprogram is finished, the variable is released from memory.

Local variables

  • A local variable is defined within a single subprogram and is only accessible to that subprogram. 
  • When this subprogram is run, the variable is added into memory.
  • When the execution of the subprogram is finished, the variable is released from memory.
  • There are two types of variables in a program, local variables and global variables. This is called the scope of the variable.
  • The scope of a global variable is the entire program.
  • The scope of a local variable is the sub-program where it has been declared.
  • A global variable on the other hand remains accessible throughout the entire program, meaning it will remain in memory indefinitely whilst the program is running.
  • Global variables are very uncommon in larger programs because they are easier to break.

Global variables

  • A local variable is defined within a single subprogram and is only accessible to that subprogram. 
  • When this subprogram is run, the variable is added into memory.
  • When the execution of the subprogram is finished, the variable is released from memory.
  • A global variable on the other hand remains accessible throughout the entire program, meaning it will remain in memory indefinitely whilst the program is running.
  • Global variables are very uncommon in larger programs because they are easier to 'break'.

Global variables

  • A local variable is defined within a single subprogram and is only accessible to that subprogram. 
  • When this subprogram is run, the variable is added into memory.
  • When the execution of the subprogram is finished, the variable is released from memory.
  • This program contains local and global variables. Anything defined outside of a subprogram (e.g. def) is a global variable:
def printIt(z):
global y
x = 20
print(x, y, z)

y = 10
printIt(5)

Local variable

Global variable

Note the importance of line 2 which contains this 'global' word. This tells Python that whenever it references the variable y (e.g. setting the value or getting the value), use the global version of it

In your own words, explain the difference between passing by reference and passing by value.

 

Open Python IDLE, create a subprogram which returns a number multiplied by 3. Your program should include a global variable which contains the number 3 and some local variables.

Task

Presentation Overview
Close
JB
Subprograms
© 2020 - 2024 J Balfour
09:27 | 21-05-2024
Join Live Session
Start Remote
Save Progress
Slideshow Outline
Presenter Mode
Widget Screen
Canvas Controls
Random Selector
Timer
Volume Meter
Binary Converter
Python Editor
Show Knox 90
Provide Feedback
Help
!
Keywords
    DragonDocs Management
    Random selector
    Sections
      Binary conversion
      Denary to binary conversion
      Binary to denary conversion
      Feedback 👍
      Accessibility

      Apply a filter:

      ×
      All slideshow files