-3

Someone else asked a similar question but I don't understand the explanation of "The return statement causes your function to exit and hand back a value to its caller." What is a caller and what value does it hand back? I'm doing codecademy and this is what has me stumped.

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)

Why do you need "return bill" and what is the difference between "return bill" and just "return"? If I get rid of the first "return bill" there is an error. I feel like "return" resets the value of "bill". If that's the case shouldn't it still work? The def tip(bill) should take the value of bill from the first part and use that instead of 100. It won't be right (because it's doing 115% of 108% instead of 115% of 100%) but it shouldn't come up with an error.

user3529201
  • 53
  • 1
  • 8
  • 3
    This question shows no research effort to learn a basic Python concept (function calling and return) that the official docs, and any good tutorial, would cover. – paisanco Oct 15 '15 at 02:45
  • 1
    TigerhawkT3: I have googled and have even included the response I found. But I do not understand this at the very basic level and do not get what the response means. – user3529201 Oct 15 '15 at 02:52
  • 1
    This is a fundamental concept that's common to almost all programming. – Barmar Oct 15 '15 at 02:55
  • 1
    With this kind of welcoming, this guy isn't coming back very soon to SO... – Igor V. May 29 '18 at 04:24

1 Answers1

-1

The return works in every language like a "response" to the code outside the function, look this line of your code:

meal_with_tax = tax(meal_cost)

The return gonna replace the "tax(meal_cost)" in it. What you are saying is that the variable "meal_with_tax" will recive the value of the variable "bill" INSIDE the function. If the variable "bill" in your function is 10, then "meal_with_tax" will be 10 aswell. If you put just "return", then your variable "meal_with_tax" will be nothing since your are saying that the response to your function is nothing.

Eranot
  • 41
  • 5