0

First, let me say I have done thorough research trying to understand. But I do not understand the explanations that others have given (which is understood by those who asked the question). This is the code I have problems with:

def tax(bill):
    """Adds 8% tax to a restaurant bill."""
    bill *= 1.08
    print "With tax: %f" % bill
    return bill

def tip(bill):
    """Adds 15% tip to a restaurant bill."""
    bill *= 1.15
    print "With tip: %f" % bill
    return bill

meal_cost = 100
meal_with_tax = tax(meal_cost)
meal_with_tip = tip(meal_with_tax)

When I delete the first "return bill" and run it, I get the first number but there is an error when it tries to calculate the second number. The def tax(bill) takes 100 and outputs 108 right? So if I delete the first "return bill", then why is def tip(bill) not doing it's calculations with 108 instead of giving an error?

I really have a problem with grasping the most basic concepts of programming. I've been learning intensely for 3 months now and this is where I am. It is a very slippery subject for my mind to grasp and I would really appreciate some help.

  • 7
    When you delete the return statement a function defaults to returning `None`. This means `meal_with_tax` has the value `None`. What does `None *= 1.15` mean? – Dair Oct 15 '15 at 03:12
  • This same question was just closed as a duplicate. http://stackoverflow.com/questions/33138918/i-dont-understand-return-in-python-and-what-is-a-caller – digitaLink Oct 15 '15 at 03:16
  • 1
    Possible duplicate of [Why would you use the return statement in Python?](http://stackoverflow.com/questions/7129285/why-would-you-use-the-return-statement-in-python) – tzaman Oct 15 '15 at 03:17
  • You just asked this question worded nearly exactly the same way: http://stackoverflow.com/questions/33138918/i-dont-understand-return-in-python-and-what-is-a-caller. You need to explain to us why the answers were insufficient, otherwise we can't help you and will just close this question as well. – Steven Rumbalski Oct 15 '15 at 03:19
  • Dair: Thank you! Now I understand. I did not know it defaults to returning None. I thought it means it changes the value to 108 and keeps it like that until changed again. digitaLink: Yes, but it was not a duplicate because I am seeking a different answer than on the other questions. – KrisKringle Oct 15 '15 at 03:19
  • 2
    You did add that "I really have a problem with grasping the most basic concepts of programming. I've been learning intensely for 3 months now and this is where I am." I would advise you find a new teacher. This is really basic stuff. If you have a good teacher already, I would advise giving up as you should have learned this by now. – Steven Rumbalski Oct 15 '15 at 03:21
  • I find it hard to imagine you have been learning "intensely" for 3 months and still don't understand returns. This is a very basic concept that you should learn in the beginning of any programming course. – digitaLink Oct 15 '15 at 03:25
  • 1
    @digitaLink see [Dunning-Kruger effect](https://en.wikipedia.org/wiki/Dunning%E2%80%93Kruger_effect). – Mark Ransom Oct 15 '15 at 03:32

1 Answers1

1

I believe you're misunderstanding the difference between a return and print statement.

In your tax(bill) function, you're multiplying bill *= 1.08. You then print the value of bill. print only outputs the value of bill to the console window -- it doesn't store that value or allow it to be used by other functions in any way.

The return statement that you're deleting returns the value stored in bill, 108, to the caller. The caller in this instance is meal_with_tax = tax(meal_cost). This means that the value of meal_with_tax is the return value of tax(bill). When you delete return bill, you're returning a value of None, so the value of meal_with_tax is None. You want to return bill so that you assign the value 108 to meal_with_tax.

The reason that tip(bill) returns an error is because it's attempting to calculate None *= 1.15. The value of bill inside tip(bill) is not 108 as you think.

Robert Lacher
  • 656
  • 1
  • 6
  • 16