Functions that Modify Parameters

In [2]:
def add_interest(balance, rate):
    new_balance = balance * (1 + rate)
    return new_balance
In [4]:
add_interest(1_000_000.00, 0.05)
Out[4]:
1050000.0
In [6]:
def add_interest2(balance, rate):
    new_balance = balance * (1 + rate)
    balance = new_balance
In [9]:
my_bal = 1_000_000.00
interest = 0.05

print(add_interest2(my_bal, interest))
print(my_bal)
None
1000000.0

SOmething

In [12]:
def add_interest(balance, rate):
    new_balance = balance * (1 + rate)
    balance = new_balance
In [13]:
def test():
    amount = 1000
    rate = 0.05
    add_interest(amount, rate)
    print(amount)
In [14]:
test()
1000

Adding interest to multiple accounts

In [25]:
def add_interest(balances, rate):
    for i in range(len(balances)):
        balances[i] = balances[i] * (1+rate)
In [26]:
def add_interest_2(balances, rate):
    new_balances = []
    for i in range(len(balances)):
        new_balances.append(balances[i] * (1+rate))
    balances = new_balances
    return balances
In [27]:
def test():
    amounts = [1000, 2200, 800, 360]
    rate = 0.05
    
    print(amounts)
    add_interest(amounts, rate)
    print(amounts)
In [28]:
test()
[1000, 2200, 800, 360]
[1050.0, 2310.0, 840.0, 378.0]