Functions




Example:
def myAdd(x, y):
    # add 2 values and return the sum
    return x+y

result = myAdd(1, 3)  # run myAdd with x set to 1, y set to 3
# store the value returned by myAdd in the result variable
print result   # what will be printed?

# run myAdd with x set to "foo", y set to "bar"
# store the returned value in the result variable
result = myAdd("foo", "bar")
print result  # what happens? what is printed?

# run myAdd with x set to 3, y set to "abc"
# store return value in result
result = myAdd(3, "abc")  
print result  # what happens?




Exercise: Write a function square() that takes a value and returns the square of that value. For example:

>>> answer = square(4)
>>> print answer
16

>>> result = square("hello")
>>> print result
hellohello




Exercise: Write a Python program which contains two functions, main() and maximumValue().