- Vocabulary
x = 5
def f(y, z):
result = x + y + z
return result
print f(1, 2) # 8
print f(3, 4) # 12
# Vocabulary:
# global variable
# local variable
# statement
# expression
# function definition (or declaration)
# function call
# parameter (or "formal parameter")
# argument
# return value
# return type
- Different Parameter and Return Types
def hypotenuse(a, b):
return ((a**2) + (b**2))**0.5
print hypotenuse(3, 4) # 5.0 (not 5)
def nearestInt(n):
return int(round(n))
print round(3.5) # 4.0 (not 4)
print nearestInt(3.5) # 4 (not 4.0)
def xor(b1, b2):
return ((b1 and (not b2)) or (b2 and (not b1))) # same as (b1 != b2)
print xor(True, True) # False
print xor(True, False) # True
print xor(False, True) # True
print xor(False, False) # False
def isPositive(n):
return (n > 0)
print isPositive(10) # True
print isPositive(-10) # False
- Function Composition
def onesDigit(n):
return n%10
def largerOnesDigit(x, y):
return max(onesDigit(x), onesDigit(y))
print largerOnesDigit(134, 672) # 4
print largerOnesDigit(132, 674) # Still 4
# Aside: we did not test our functions very well. Consider:
print largerOnesDigit(-12, 42) # 8 (what went wrong? how can we fix it?)
####################################
# Another composition example
def f(w):
return 2*w + 3
def g(x, y):
return 3*x - 2*y
def h(z):
return g(z, z+1) + f(g(2, z))
print f(3)
print g(3, 4)
print h(5)
- Local Variable Scope
def f(x):
print "In f, x =", x
x += 5
return x
def g(x):
return f(x*2) + f(x*3)
print g(2)
######################################
# Another example:
def f(x):
print "In f, x =", x
x += 7
return x / 3
def g(x):
x *= 10
return 2 * f(x)
def h(x):
x += 3
return f(x+4) + g(x)
print h(f(1))
- Global Variable Scope
# In general, you should avoid using global variables.
# You will even lose style points if you use them!
# Still, you need to understand how they work, since others
# will use them, and there may also be some very few occasions
# where you should use them, too!
g = 100
def f(x):
return x + g
print f(5) # 105
print f(6) # 106
print g # 100
######################################
# Another example:
g = 100
def f(x):
# If we modify a global variable, we must declare it as global.
# Otherwise, Python will assume it is a local variable.
global g
g += 1
return x + g
print f(5) # 106
print f(6) # 108
print g # 102
- Return Statements
# return ends the function immedately
def f(x):
return x+5
print "This line, and the one after it, will never run!"
return 42
print f(5) # 10
# No return statement --> return None
def f(x):
z = x + 42
print f(5) # None
# Another example
def f(x):
x + 42
print f(5) # None
- Test Functions
- A broken test function
def onesDigit(n):
return n%10
def testOnesDigit():
print "Testing onesDigit()...",
assert(onesDigit(5) == 5)
assert(onesDigit(123) == 3)
assert(onesDigit(100) == 0)
assert(onesDigit(999) == 9)
print "Passed!"
testOnesDigit() # Passed!
- A better version
def onesDigit(n):
return n%10
def testOnesDigit():
print "Testing onesDigit()...",
assert(onesDigit(5) == 5)
assert(onesDigit(123) == 3)
assert(onesDigit(100) == 0)
assert(onesDigit(999) == 9)
assert(onesDigit(-123) == 3) # Added this test
print "Passed!"
testOnesDigit() # Crashed! So the test function worked!