0% found this document useful (0 votes)
3 views13 pages

lectures_cip3_lecture-slides-9_file

The document discusses the importance of understanding information flow and parameter passing in functions, highlighting the differences between buggy and functional code examples. It emphasizes that when parameters are passed to functions, new variables are created, which may not be linked to the original variables. Additionally, it introduces the encapsulation principle, stating that data used by a function should be encapsulated within the function to avoid unintended side effects.

Uploaded by

Leonardo Bertati
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views13 pages

lectures_cip3_lecture-slides-9_file

The document discusses the importance of understanding information flow and parameter passing in functions, highlighting the differences between buggy and functional code examples. It emphasizes that when parameters are passed to functions, new variables are created, which may not be linked to the original variables. Additionally, it introduces the encapsulation principle, stating that data used by a function should be encapsulated within the function to avoid unintended side effects.

Uploaded by

Leonardo Bertati
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Today's Goals

1. Understanding information flow


Bad Times With functions
Le
# NOTE: This program is buggy!! thi t’s “
s p tra
rog ce”
def add_five(x): ram
x += 5

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 main(): When we want to


x = 3 “reassign” x inside a
x = add_five(x) helper function,
print("x = " + str(x)) employ the
x = change(x)
pattern!
Parameter passing mechanism

When a parameter is passed during a


function call, a new variable is
created for the lifetime of the
function call.

That new variable may or may not


have the same name as the value that
was passed in!
No inherent connection between these two

# NOTE: This program is buggy!!

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 deposit(amount): Different variables with the same name!


balance += amount
Use return to pass back information

Don't want using your toaster


to impact your refrigerator!

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 deposit(balance, amount):


balance += amount
return balance
Encapsulation Principle

Don't want using your toaster


to impact your refrigerator!

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 deposit(balance, amount): Encapsulation Principle: Data


balance += amount used by a function should be a
return balance constant, a parameter or a (local)
variable encapsulated in function

You might also like