lectures_cip3_lecture-slides-9_file
lectures_cip3_lecture-slides-9_file
def main():
x = 3
add_five(x)
print("x = " + str(x))
Bad Times With functions
# NOTE: This program is buggy!!
def add_five(x):
x += 5
def main():
x = 3
add_five(x)
print("x = " + str(x))
Good Times With functions
# NOTE: This program is feeling just fine...
def add_five(x):
x += 5
return x
def main():
x = 3
x = add_five(x)
print("x = " + str(x))
Good Times With functions
# NOTE: This program is feeling just fine...
def add_five(x):
x += 5
return x
def add_five(x):
x += 5
These are two
separate variables.
They are not linked!
def main():
x = 3 Only relationship:
value of main’s x is
add_five(x) used when creating
print("x = " + str(x)) add_five’s x
Later on in class… we will see cases
where changes to variables in helper
functions seem to persist! It will be
great. We will let you know when we
get there and exactly when that
happens!
Careful!
Reviewing Parameters and
information flow
Referring to Variables in Functions
def main():
balance = int(input("Initial balance: "))
while True:
amount = int(input("Deposit (0 to quit): "))
if amount == 0:
break
deposit(amount)
def main():
balance = int(input("Initial balance: "))
while True:
amount = int(input("Deposit (0 to quit): "))
if amount == 0:
break
balance = deposit(balance, amount)
def main():
balance = int(input("Initial balance: "))
while True:
amount = int(input("Deposit (0 to quit): "))
if amount == 0:
break
balance = deposit(balance, amount)