# File: args2.py # Program to demonstrate how functions handle variables # # Matt Bishop, ECS 10, Spring 2014 # # # this one shows passing parameters # it just prints the 3 arguments def func1(a, b, c): print("Argument #1:", a) print("Argument #2:", b) print("Argument #3:", c) y = int(b) # # this one defines a local variable, x # and assigns a value to it def func2(): x = 9 print("func2: x =", x) # # this one changes the value of a parameter def func3(y): print("func3: y =", y) y = 3456 print("func3: y =", y) # # this one changes the value of a parameter def func4(y): print("func4: y =", y[0]) y[0] = 3456 print("func4: y =", y[0]) # # the main program # def main(): # first, show how argumets are passed func1(7, "3", -3.14159) # now, show how local variables doesn't # affect variables in the caller x = 6 print("main: x =", x) func2() print("main: x =", x) # show how changing parameters # doesn't affect variables in the caller y = 21 print("main: y =", y) func3(y) print("main: y =", y) # finally, show how changing parameters # that are list elements does affect # variables in the caller y = [ 21 ] print("main: y =", y) func4(y) print("main: y =", y) main()