0

I'm learning with Zelle's Python Programming and got stuck a little bit on functions.

We got this:

def addInterest(balance, rate):
    newBalance = balance * (1+rate)
    balance = newBalance

def test(): 
    amount = 1000
    rate = 0.05
    addInterest(amount, rate)
    print amount

test()

This code fails to print 1050 as output. But the below succeedes:

def addInterest(balance, rate):
    newBalance = balance * (1+rate)
    return newBalance

def test():
    amount = 1000
    rate = 0.05
    amount = addInterest(amount, rate)
    print amount

test() 

The subtle difference lies in the line 3rd of the addInterest function. Zelle explains this but I'm yet to get the hang of it. Can you explain please why the #1 code - being almost idential - does not do what #2 does?

nutship
  • 4,624
  • 13
  • 47
  • 64

3 Answers3

3

That's because the balance object you modify inside addInterest is not the same as the amount object you pass to the function. To be short, you modify the local copy of an object, passed to the function, so the value of original object remains intact. You can see it if you run the following code in your python shell:

>>> def addInterest(balance, rate):
...     print (balance)
...     newBalance = balance * (1 + rate)
...     balance = newBalance
... 
>>> amount = 1000
>>> rate = 0.05
>>> print id(amount)
26799216
>>> addInterest(amount, rate)
1000
>>> 

The id function returns the identity of an object, which can be used to test if two object are the same.

nameless
  • 1,969
  • 11
  • 34
0

The key word is return. Return is used to, pretty much, return a value from a function to a variable (in your second code, the variable is amount).

In #1 you aren't returning anything, and that is why print amount is not what you want it to be.

In your second code, you are returning newBalance's value. The variable amount is now the same value as newBalance in the function.

So in your first code, doing addInterest(amount, rate) does nothing. It's not returning anything, so it would have no use. What you have done in your second function is however correct.

TerryA
  • 58,805
  • 11
  • 114
  • 143
  • I don't think so. The question has nothing to do with "return". – georg Jan 26 '13 at 11:36
  • What do you mean? The reason why the second function works and not the first one is because the function in the first code doesn't return anything :p. The OP asked why #1 doesn't do what #2 does, specifically mentioning that it had something to do with the `return` line, and so that's where return comes in. – TerryA Jan 26 '13 at 11:40
  • It's pretty much obvious that the first version doesn't use `return` and doesn't work. The question is why exactly it doesn't work this way. – georg Jan 26 '13 at 11:50
0

Check the beautiful answers on How do I pass a variable by reference?

    self.variable = 'Original'
    self.Change(self.variable)

def Change(self, var):
    var = 'Changed'
Community
  • 1
  • 1
0x90
  • 39,472
  • 36
  • 165
  • 245