Download presentation
Presentation is loading. Please wait.
Published byBryan Morris Modified over 9 years ago
1
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Lecture 17 Parameters, Scope, Return values
2
2 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.
3
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.
4
4 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.
5
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 = 5 addTwo( )
6
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.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 Example:
7
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.
8
8 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?
9
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.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
10
10 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. 1 2 3 myList theList
11
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.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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.