Previous Lecture Lecture 9 Next Lecture

Lecture 9, Tue 02/06

Functions

Recorded Lecture: 2_6_24

Function return Statement

def lotsOfReturns():
	print("before return 1")
	return 1
	print("after return 1") # will never print (might as well delete)
	return 2 # Will never execute (might as well delete)

print(lotsOfReturns()) # 1
def lotsOfReturns():
	print("before return 1")
	return # None is returned
	print("after return 1") # will never print (might as well delete)
	return # Will never execute (might as well delete)

print(lotsOfReturns()) # None

Practice

def getClassStanding(units):
	''' Returns a string of either Freshman, Sophomore, Junior,
		Senior depending on the units '''

	if units < 50:
		return "Freshman"
	elif units < 90:
		return "Sophomore"
	elif units < 135:
		return "Junior"
	else:
		return "Senior"

numUnits = int(input("Enter number of units: "))
print(getClassStanding(numUnits))
def getClassStanding(units):
	''' Returns a string of either Freshman, Sophomore, Junior,
		Senior depending on the units '''

	if units < 50:
		return "Freshman"
	if units < 90:
		return "Sophomore"
	if units < 135:
		return "Junior"
	else:
		return "Senior"

biggestInt Example

def biggestInt(a, b, c, d):
	''' Function that will return the biggest value
		(a, b, c, or d) '''
	biggest = 0
	if a >= b and a >= c and a >= d:
		biggest = a
	if b >= a and b >= c and b >= d:
		biggest = b
	if c >= a and c >= b and c >= d:
		biggest = c
	else:
		biggest = d
	return biggest

assert biggestInt(1,2,3,4) == 4
assert biggestInt(1,2,4,3) == 4
assert biggestInt(1,4,2,3) == 4 # error
assert biggestInt(4,1,2,3) == 4 # error
def biggestInt(a, b, c, d):
	''' Function that will return the biggest value
		(a, b, c, or d) '''

	if a >= b and a >= c and a >= d:
		return a
	if b >= a and b >= c and b >= d:
		return b
	if c >= a and c >= b and c >= d:
		return c
	return d