1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Lecture 17 Parameters, Scope, Return...

11
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Lecture 17 Parameters, Scope, Return values

Transcript of 1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Lecture 17 Parameters, Scope, Return...

Page 1: 1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Lecture 17 Parameters, Scope, Return values.

1

Computer Science of Graphics and Games

MONT 105S, Spring 2009Lecture 17

Parameters, Scope, Return values

Page 2: 1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Lecture 17 Parameters, Scope, Return values.

2

Recall Functions

# program: volumeFinder.pydef WriteCylinderVolume( r, h):

volume = 3.14159 * r * r * hprint "The volume is", volume, "cc"

#Program startradius = 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.

Page 3: 1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Lecture 17 Parameters, Scope, Return values.

3

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.

Page 4: 1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Lecture 17 Parameters, Scope, Return values.

4

Global VariablesA 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.

Page 5: 1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Lecture 17 Parameters, Scope, Return values.

5

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 = 5addTwo( )

Page 6: 1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Lecture 17 Parameters, Scope, Return values.

6

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.

# program calcCube.pydef Cube( n):

result = n * n * nreturn result

#Program starts herenumber = input("Please enter a number: ")answer = Cube(number)print "The cube of", number, "is", answer

Example:

Page 7: 1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Lecture 17 Parameters, Scope, Return values.

7

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.

Page 8: 1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Lecture 17 Parameters, Scope, Return values.

8

Passing a number as a parameter

# program doubleTrouble.pydef double (num ):

num = num * 2print "The double is", num

# Program begins herenumber = 3double(number)print "The number is", number

#What is the output?

Page 9: 1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Lecture 17 Parameters, Scope, Return values.

9

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.pydef double2(myList):

myList[0] = myList[0] * 2print "The elements of myList are: ", myList

#The program begins heretheList = [1, 2, 3]print "The elements of theList are: ", theListdouble2(theList)print "The elements of theList are now ", theList

Page 10: 1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Lecture 17 Parameters, Scope, Return values.

10

Execution of doubleTroubleWhen 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.

1 2 3

myList theList

Page 11: 1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Lecture 17 Parameters, Scope, Return values.

11

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.pydef double3(myList):

myList = [4, 5, 6]print "The elements of myList are: ", myList

#The program begins heretheList = [1, 2, 3]print "The elements of theList are: ", theListdouble3(theList)print "The elements of theList are now ", theList