Python Programming: An Introduction To Computer Science: Defining Functions
Python Programming: An Introduction To Computer Science: Defining Functions
An Introduction to
Computer Science
Chapter 6
Defining Functions
Gives us this…
>>> main()
Happy birthday to you!
Happy birthday to you!
Happy birthday, dear Fred...
Happy birthday to you!
window = win
year = 0
height = principal
variables called
rate = 0.05
addInterest(amount, rate)
line of rate)
balance = newBalance
addInterest
def test():
creates a new amount = 1000
balance is then
assigned the value
of newBalance.
def test():
amount = 1000
rate = 0.05
amount = addInterest(amount, rate)
print(amount)
test()
Python Programming, 2/e 63
Functions that Modify
Parameters
Instead of looking at a single account, say we
are writing a program for a bank that deals
with many accounts. We could store the
account balances in a list, then add the
accrued interest to each of the balances in
the list.
We could update the first balance in the list
with code like:
balances[0] = balances[0] * (1 + rate)
def test():
amounts = [1000, 2200, 800, 360]
rate = 0.05
addInterest(amounts, 0.05)
print(amounts)
test()
and rate.
amounts = [1000, 2200, 800, 360]
rate = 0.05
addInterest(amounts, 0.05)
The value of the print(amounts)
variable amounts is
a list object that
contains four int
values.
1, …, length –1 and
addInterest(amounts, 0.05)
print(amounts)