MC logo

Functions, Parameters, and Globals


CS 233 Python Lecture Examples

<< Download >>
#!/usr/bin/python

# References inside functions are local...
snoggle = 17
def wongle(n):
    snoggle = n

print 'A:', snoggle,
wongle(235)
print snoggle

# ...unless declared global.
def wangle(n):
    global snoggle
    snoggle = n

print 'B:', snoggle,
wangle(235)
print snoggle

# Arguments are pass-by-value...
def snapple(n):
    n = 55

print 'C:', snoggle,
wangle(snoggle)
print snoggle

# ...except for the contents objects, such as lists...
def snarffle(z):
    z.append(22)

toggle = [ 'a', 'b', 'c' ];
print 'D:', toggle,
snarffle(toggle)
print toggle

# ...which means the contents of the object, not the parameter.
def snarggle(z):
    z = [ 4, 5 ]

print 'F:', toggle,
snarggle(toggle)
print toggle

Argument passing in Python is a lot like Java. Arguments are passed by value, so that changing a parameter does not change the value at the caller. However, objects (including built-in types which are represented as objects) are passed as references, so using the the paramater to change them will make changes visible to the caller.

<<
>>