Session 17 - Computer Science

Download Report

Transcript Session 17 - Computer Science

Computer Science of
Graphics and Games
MONT 105S, Spring 2009
Lecture 17
Parameters, Scope, Return values
1
Recall Functions
# program: volumeFinder.py
def WriteCylinderVolume( r, h):
volume = 3.14159 * r * r * h
print "The volume is", volume, "cc"
#Program start
radius = input("Enter the radius:")
height = input("Enter the height:")
WriteCylinderVolume(radius, height)
Actual parameters are matched up in order with the formal
parameters.
The value of radius is passed to the parameter r.
The value of height is passed to the parameter h.
2
Scope of local Variables for
functions
The variable named volume in the previous example is a local
variable for the WriteCylinderVolume( ) function.
Because volume is defined within the function, it is invisible
outside the function (e.g. in the main program).
The scope of the volume variable is limited to the function.
The scope of a function's parameters (e.g. r and h) is also
limited to the function.
3
Global Variables
A variable that is defined outside any function is visible to all
functions and to the main program.
These variables are global variables.
Example:
yertle = Turtle( )
def drawX( ):
yertle.forward(150)
...
def drawO( ):
yertle.goto(0, 0)
...
Both functions can make use of yertle.
4
Changing the value of a global
variable
You cannot change the value of a global variable within a function
without first gaining access to that variable with the keyword,
global.
def addTwo( ):
global radius
radius = radius + 2
print radius
radius = 5
addTwo( )
5
Return Values
•Sometimes we would like a function to return information to the calling
program.
•We can do this using a return value, specified by the keyword, return.
•Once the return statement is reached, the interpreter immediately returns
execution to the calling program.
Example:
# program calcCube.py
def Cube( n):
result = n * n * n
return result
#Program starts here
number = input("Please enter a number: ")
answer = Cube(number)
print "The cube of", number, "is", answer
6
Passing Parameters
When we pass a parameter to a function, Python passes a
reference to the parameter.
If the parameter is an immutable type, including strings,
integers and floating point numbers, the function cannot
change the value of the variable in the calling program.
If the parameter is a mutable type, such as a list, the function
can change the value of individual list elements and this will
be reflected in the calling program.
7
Passing a number as a
parameter
# program doubleTrouble.py
def double (num ):
num = num * 2
print "The double is", num
# Program begins here
number = 3
double(number)
print "The number is", number
#What is the output?
8
Passing a List as a Parameter
When we pass a list to a function, any changes to list elements
will be reflected in the calling program.
Example:
# program doubleTrouble.py
def double2(myList):
myList[0] = myList[0] * 2
print "The elements of myList are: ", myList
#The program begins here
theList = [1, 2, 3]
print "The elements of theList are: ", theList
double2(theList)
print "The elements of theList are now ", theList
9
Execution of doubleTrouble
When double2( ) is called, the address of theList is passed to
the parameter, myList.
myList and theList label the same location in memory.
Because a list is mutable, we can change an individual
element in myList, and this is the same as changing it in
theList.
theList
myList
1
2
3
10
Lists aren't so simple...
When we assign a new value to the entire list, it does not
affect the value in the calling program. A new reference is
generated.
# program doubleTrouble2.py
def double3(myList):
myList = [4, 5, 6]
print "The elements of myList are: ", myList
#The program begins here
theList = [1, 2, 3]
print "The elements of theList are: ", theList
double3(theList)
print "The elements of theList are now ", theList
11