Computer Science 15-110, Spring 2011
Class Notes: Writing Functions
Writing Functions
def minSquared(x,y):
smaller = min(x,y)
return smaller*smaller
print minSquared(3,4)
print minSquared(4,3)
Another example:
def isEvenPositive(x):
isEven = ((x % 2) == 0)
isPositive = (x > 0)
return (isEven and isPositive)
print(isEvenPositive(-2))
print(isEvenPositive(-1))
print(isEvenPositive(0))
print(isEvenPositive(1))
print(isEvenPositive(2))
Yet another example:
def initials(firstName, lastName):
firstInitial = firstName[0]
lastInitial = lastName[0]
return firstInitial + lastInitial
print initials("Douglas", "Adams")
carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem