# Example code for Day 12 Lecture, CS 111 Spring 2014 # return vs. print def foo(): return "hello, I'm a string in foo!" def bar(): print "hello, I'm a string in bar!" s1 = foo() s2 = bar() print "s1: ", s1 print "s2: ", s2 # Calling a function from a function defined earlier def foo(): wuz = bar("hello") return 5 + wuz def bar(ack): r = len(ack) / 10.0 for i in range(len(ack)): r = (r**0.5) % 1.0 return r zug = foo() print type(zug) print zug # Multiple arguments def greet(name, repeats): for i in range(repeats): print "Hello %s!" % name # Default argument values def greet(name, repeats=1): for i in range(repeats): print "Hello %s!" % name # Scoping (shadowing) def xPlusOne(x): return x + 1 x = 6 y = xPlusOne(28) print x, y # Can't modify arguments! def addOneToX(x): print "In the function, x is", x x = x + 1 print "In the function, after modifying, x is", x x = 6 print "Before the function call, x is", x addOneToX(x) print "After the function call, x is", x