Objectives

Students will be able to:

  • understand that a variable is only visible to the function it is declared in.
  • understand that data can be passed as arguments to other functions and procedures
  • understand that a function or procedure will have a parameter list in its definition to accept arguments for processing
  • understand that global variables can be used which are visible to an entire program
  • understand that constants can be used when data being stored must not be changed
  • understand that global variables should be used sparingly

Scope

Check out this classic computer program:

Note: in this page, we will refer to functions and procedures as modules.

As you can see, it doesn't work! That's because the variable name is declared inside a module called main. This variable is not visible to other modules in the program.

A variable has local scope if it is declared inside a module. Only that module can see it.

So, in the above program, name is declared in the main() module and the intro() module cannot see it.

The best way around this problem is to pass a variable as an argument to other modules in our program.

Prediction

Look at this program. What do you think will be displayed in the output? Write it down on a piece of paper.

def main():

age = 17   #local variable in main module

print("In the main procedure, you are",age,"years old!")

birthday(age)  #the birthday module accepts age as an argument

print("In the main procedure, you are",age,"years old!")

 

def birthday(num):   #the birthday module's design requires one parameter

num = num+1

print("Happy birthday! You are",num,"years old!")

 

#call the main procedure

main()

Code this program. Was your prediction correct?

So, in Python, a function or procedure can accept data via its parameter and change the data stored in the parameter. However, the change does not affect the original argument.

This form of argument passing is commonly called PASS BY VALUE. I like to think of it as GET A COPY but it is known formally as PASS By VALUE.

You know that to update the original argument, we can design a function and return a value eg.

def main():

age = 17

print("You are",age,"years old!")

age = birthday(age)

print("Happy birthday. Now you are",age,"years old!")

 

def birthday(num):

num = num+1

return num

 

#call the main procedure

main()

Global Scope

Sometimes it is useful to have variables that are visible to the entire program.

These are known as global variables. They have global scope.

Let's redesign the orginal program at the top of this unit so that it works using a global variable:

#create a global variable and assign it some data.Note it is not in any module - it's GLOBAL!

name = input("Enter your name:")

 

def main():

print("Welcome to this program.")

intro()

 

def intro():

print("Hello",name,". Nice to meet you!")

 

#call the main procedure

main()

Note that name is not declared in any function or procedure. It is visible to all functions and procedures.

It is a global variable. It has global scope.

Note: in Python, if you want to update a global variable from within a function, you have to let Python know.

This is how to do it:

#declare a global variable

age = int(input("Enter your age:"))

def main():

print("You are",age,"years old!")

birthday(age)

print("Happy Birthday! You are now",age,"years old!")

 

#this function will update the global variable

def birthday():

global age

age = age+1

 

#call the main procedure

main()

Warning!

In big programs, having lots of global variables can cause headaches. Imagine if hundreds of programming statements use a global variable. If there is a problem with the code, it is very hard to track where the problem is coming from.

Also, if a function has been designed to use a global variable and then that function is used in another program that doesn't use global variables, it will have to be redesigned. This is generally unnecessary - just design the function with a parameter(s) instead!

Programs that use global variables can be hard to understand. If the value stored in a global variable is dependant on many statements throughout the program, you have to have an understanding of the whole program to see what is going on. Programmers on big projects are usually not familiar with the whole program. It is easier to design a function or procedure knowing what data is coming in (arguments) and what to return.

Tags

module procedure return identifier parameter pseudocode return typedefinecalldata type